Hello
I have problem with generating random number as a variable, or usable under a variable. I have run into this problem several times, so I guess there is something wrong in the way I understand Random.Range.
For example I would want to instantiate object from one of 4 spawn points chosen at random. Any time I do
var blahBlah = (Random.Range(1,4));
objects spawn from first spawn point and I get message “Random.Range int can be called only from main thread.” I understand that I cant get random number under variable… but is there a way to get the random number in there?
Thank you both very very much. I have not been doing it like this. I have been trying to ram the random range directly into var blahBlah. it works now. I feel like I have been trying go squeeze square peg through a round hole Thank you both again
Just to clarify, the full text of the exception is:
The relevant bit is “field initializers”, which are the parts of a class that are not inside functions.
You can move the initialization to Start() or Awake(), as others have suggested, and then the value will be different every time the scene is run. If you want it to be initialized only once, and be the same between runs, then you can move the the initialization to Reset(). Reset() gets called in the editor when you first attach a script, or choose it from the context menu.