Question is off-topic or not relevant
Random.Range kepps returing the same value
When I put it in a loop it keeps telling me the same answer! This is my code :
[SerializeField] float Top = 26.8f;
[SerializeField] float Bottom = 6.18f;
[SerializeField] float fallSpeed = 7.5f;
[SerializeField] float zValue = 1.28f;
[SerializeField] int fallRange = 0;
void Update () {
transform.Translate (Vector3.down * fallSpeed * Time.deltaTime);
if (transform.localPosition.y <= Bottom){
fallRange = (int)Random.Range (-10, 10);
transform.position = new Vector3 (fallRange, Top, zValue);
print ("Random number = " + fallRange);
}
}
}
Downvoted because this kind of question again fits into a strange pattern which looks like a bot created question. Duplicated title and duplicated question text. User newly registrated, posted this one question and wasn't seen since then...
Well then... im not a bot and im stuck with my game! I A$$anonymous$$ HU$$anonymous$$AN. So you know im just bad at program$$anonymous$$g and i just need help
Answer by Bunny83 · Jan 09, 2018 at 11:05 AM
In case this is mens as a serious problem, we can't see any problem in the code that has been posted. Random.Range does for sure work properly. If it returns the same value every time you have to set a constant seed each time before you call Random.Range or you reset the internal state manually somewhere.
ps: The int cast is unnecessary:
fallRange = (int)Random.Range (-10, 10);
as Range as two overloads, one that takes float values which return a float value and one that takes int values and returns an int. Since you pass two int values (-10 and 10) the returned value is an int in the range -10 to 9
You said it returns the same value. Which value?