x


random not working

I am attempting to create a character generator script. I'm making private variables to grab the names from the enum list. That is working, but when I try to define a public variable with a random int and place it in the OnGUI, it doesn't like it whether I put the definitions in awake or start.

Here is the code:

using UnityEngine;
using System.Collections;
using System; 

public class CharacterGenerator : MonoBehaviour
{
    public const float VERSION=.1f;
    public bool ClearPrefs=false;
    private string _constitution= Attributes.Stat.Constitution.ToString();
    private string _strength= Attributes.Stat.Strength.ToString();
    private string _stamina= Attributes.Stat.Stamina.ToString();    
    private string _dexterity= Attributes.Stat.Dexterity.ToString();       
    private string _intelligence= Attributes.Stat.Intelligence.ToString();  
    private string _wisdom= Attributes.Stat.Wisdom.ToString();
    private string _focus= Attributes.Stat.Focus.ToString();

    // Use this for initialization
    void Start ()
    {
        public Constitution=Random.Range(1, 20); //error here with red line 
        //public int Strength= UnityEngine.Random.Range(1, 20);
        //public int Stamina= UnityEngine.Random.Range(1, 20);
        //public int Dexterity= UnityEngine.Random.Range(1, 20);
        //public int Intelligence= UnityEngine.Random.Range(1, 20);
        //public int Wisdom= UnityEngine.Random.Range(1, 20);
        //public int Focus= UnityEngine.Random.Range(1, 20);
    }

    // Update is called once per frame
    void Update ()
    {
        public Constitution=Random.Range(1, 20); here too
    }

    void GenerateStats()
    {
        GUI.Label (new Rect (40, 50, 700, 50), _constitution + ":" + Constitution ); 
        GUI.Label (new Rect (40, 70, 700, 50), _strength + ":" + Strength);
        GUI.Label (new Rect (40, 90, 700, 50), _stamina + ":" + Stamina);
        GUI.Label (new Rect (40, 110, 700, 50), _dexterity + ":" + Dexterity);
        GUI.Label (new Rect (40, 130, 700, 50), _intelligence + ":" + Intelligence);
        GUI.Label (new Rect (40, 150, 700, 50), _wisdom + ":" + Wisdom);
        GUI.Label (new Rect (40, 170, 700, 50), _focus + ":" + Focus);
    }

    void OnGUI()
    {
        GUI.Label (new Rect (Screen.width/2, 10, 700, 50), "True Calling Character Generator!");
        if(GUI.Button (new Rect (250, 300, 100, 50), "Generate Stats")){
            Debug.Log("Generate our Stats!");
            GenerateStats();
        }
        if (GUI.Button (new Rect (355, 300, 100, 50), "Start Game")){
            Debug.Log("Start Our Game!");
        }
    }
}

Only the OnGUI stuff is showing up. It worked once when everything was in the OnGUI function, but when I tried it again, I got an exception and zero values. What am I doing, not doing, should be doing? Thanks!

more ▼

asked Mar 29 '12 at 09:47 PM

Gilead7 gravatar image

Gilead7
117 18 33 42

One thing you should be doing: edit your question, select your code, hit the 010/101 button to format it

Mar 30 '12 at 12:18 AM DaveA

Thanks! I didn't even know that was there.

Apr 02 '12 at 07:53 PM Gilead7
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Constitution needs to be declared as an int.

Your code should be

public int Constitution=(int)Random.Range(1,20);

Random.Range should also be casted to an int because that method returns a float.

Also you can't declare a public variable inside a function. Local variables are automatically private.

And finally, a good code practice is to use camelcase for variable names and only start with a capital letter if you're referring to a class.

more ▼

answered Mar 30 '12 at 12:40 AM

xellow gravatar image

xellow
20 1

Kinda what I said

Mar 30 '12 at 12:42 AM DaveA

My post had to be approved by a moderator and there weren't any answers yet when I wrote up my response. Sorry for the repeat.

Mar 30 '12 at 01:03 AM xellow
(comments are locked)
10|3000 characters needed characters left

public int Constituion (or public float Constitution) for one. Also put that public int Constitution (that's called a 'declaration') outside of the other functions at the top (but inside the class). Like right above Start. But set the value in Start and Update, don't bother assigning it a random value in the declaration.

more ▼

answered Mar 30 '12 at 12:20 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

I had originally placed it there, but the editor suggested placing it in update or start, but it didn't work there either.

Now it's saying random.range can only be called from the main thread. What does that mean?

Apr 02 '12 at 07:55 PM Gilead7

No, you interpreted the error the wrong way. It was telling you that you can use a function (in this case Random.Range) as a field-initializer. Only constant values can be used there. So it suggested to initialize your variable in Start, but not to remove the class variable.

public class CharacterGenerator : MonoBehaviour
{
    // [...]
    public int Constitution;  // variable declaration

    void Start ()
    {
        Constitution = Random.Range(1, 20); // variable initialization
Apr 02 '12 at 08:40 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4371
x252
x59

asked: Mar 29 '12 at 09:47 PM

Seen: 1273 times

Last Updated: Apr 02 '12 at 08:40 PM