- Home /
Random variable values?
Hello, is there any way to randomly assign a (float) variables values between a minimum and a max?
What I tried is to have a float like public float example = 20f
and generate a random range in the start function, variablize it, and then instead do public float example = "variable"
Another user told me this for a similar project, and it worked, but when I apply it now, it says "variable" does not exist in the current context. This is probably because the public float is assigned before the random number is varablized, but when I try to move it after the start function, even more errors appear. Is there any code to work around this?
Thanks!
Answer by Bunny83 · Sep 14, 2020 at 02:23 AM
What the heck do you mean by "varablized"? A variable is a named piece of memory. Variables can not be created dynamically. Your example also doesn't make much sense. When you have a member variable like this:
public float example = initialValue;
the field initializer (the = initialValue
part) is executed before the constructor of the class actually runs and therefore long before any Start method might be called. Your next issue is that the field initializer is only used once for serialized fields- Since serialized fields get their value serialized in the inspector. So, once serialized, the value you assign in the field initializer becomes meaningless since it's overwritten by the serialized value anyways.
Why don't you just roll your random value in Start and assign it to the variable of your choice? In your case "example" IS your target variable.
Hi, sorry for the late answer, and thanks for answering. I'm not sure what you mean about rolling my random value in start and assigning to my variable. What I'm doing right now is this,
public float degreesPerSecond = randomLocation;
public float $$anonymous$$Spawn = 0;
public float maxSpawn = 10;
void Start()
{
float randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
}
Which yields the error: The name 'randomLocation' does not exist in the current context. Can you please elaborate about what you mean? Thanks.
Try creating the randomLocation variable in the class instead of the Start() method.
public float $$anonymous$$Spawn = 0;
public float maxSpawn = 10;
private float randomLocation;
Then, in start just set a value to it.
randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
Let me know if that helps.
Thanks for the answer! However, when I apply the code you gave me, it still says "The name 'randomLocation' does not exist in the current context." From what I understand, the code you gave me is this:
private float example = randomLocation;
public float $$anonymous$$Spawn = 0;
public float maxSpawn = 10;
void Start()
{
float randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
}
I also tried this, in case that's what you meant:
public float $$anonymous$$Spawn = 0;
public float maxSpawn = 10;
public float example = randomLocation;
private float randomLocation;
void Start()
{
float randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
}
But this yields the error A field initializer cannot reference the non-static field, method, or property 'Floater.randomLocation'
*note floater is just my script name.
Am I doing something wrong, if so what? Thanks for the help.
Answer by UTus · Sep 24, 2020 at 07:45 AM
Get Random variable values
var randomValue = Random.Range(min, max);
Get Random enum type
public static T RandomEnum<T>()
{
Array values = Enum.GetValues(typeof(T));
return (T)values.GetValue(new System.Random().Next(0, values.Length));
}