- Home /
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!
One thing you should be doing: edit your question, select your code, hit the 010/101 button to format it
Answer by xellow · Mar 30, 2012 at 12:40 AM
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.
$$anonymous$$y 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.
Answer by DaveA · Mar 30, 2012 at 12:20 AM
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.
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?
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 : $$anonymous$$onoBehaviour
{
// [...]
public int Constitution; // variable declaration
void Start ()
{
Constitution = Random.Range(1, 20); // variable initialization
Your answer
![](https://koobas.hobune.stream/wayback/20220613065712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Randomly Select String from Array 0 Answers
What is the best way to grab and move the immediate children of a GameObject. 1 Answer
Random.Range, GameObjects and Length 1 Answer