- Home /
Random.Range is not changing?
I want my object to move to a random point on the X axis between -50 & 50 at start-up. The value is random, but it returns the same value each time.
void Start()
{
transform.position = new Vector3(Random.Range(-50, 50), transform.position.y, transform.position.z);
}
Also, if I put this in void Update() it works fine.
Answer by Anxo · Oct 24, 2014 at 08:14 PM
could it be that you are overwriting it somewhere after start?
Check to make sure the Xvalue is random before you assign it, it will get you closer to your answer.
void Start(){
randomNumber = Random.Range(-50f,50f);
Debug.Log("Random Number is = "+ randomNumber); // This will tell us if random is workin.g
transform.position = new Vector3(randomNumber,transform.position.y,transform.position.z);
}
WUCC check for errors
I found out that it is generating random numbers, but I have the script attached to multiple objects and the numbers are changing relative to the other objects.
For example, if Object1 generates a 3, then Object2 will generate a 1. If Object1 generates a 7, then Object 2 generates a 5.
Do I need to have a separate script for each object?
I have found the solution!
$$anonymous$$y problem was that multiple objects were using the same script to generate a number, and so I got similar values each time. I created a new script for each object and it works fine.
Thank you, @Anxo for leading me in the right direction.
You are welcome, please close the question by hitting the checkbox so people know it is resolved.Thank you.
Your answer
Follow this Question
Related Questions
Random.Range doesnt work anymore 4 Answers
RNG State Changes When Loop Iteration is Skipped? 0 Answers
Random Sound When Click? 0 Answers
How to randomize answers? 1 Answer
How to make Random.Range() step by a certain amount? 1 Answer