|
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: 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!
(comments are locked)
|
|
Constitution needs to be declared as an int. Your code should be
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. 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)
|
|
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?
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.
Apr 02 '12 at 08:40 PM
Bunny83
(comments are locked)
|

One thing you should be doing: edit your question, select your code, hit the 010/101 button to format it
Thanks! I didn't even know that was there.