- Home /
Random Time question
I have the following script and I want that the delay time to be randomly, a little bit of help will be appreciate
public Sprite BlackCircle; public Sprite RedCircle;
float timer = 1f; float delay = 1f;
void Update() { timer -= Time.deltaTime; if ( timer <= 0) { if(this.gameObject.GetComponent().sprite == BlackCircle) { this.gameObject.GetComponent().sprite == RedCircle; timer = delay; return; } if(this.gameObject.GetComponent().sprite == RedCircle) { this.gameObject.GetComponent().sprite == BlackCircle; timer = delay; return; }
Answer by Commoble · Mar 12, 2017 at 04:26 PM
You can use Unity's Random class or the System.Random class to get a random float in a particular range and set your timer to that value instead of 1F whenever it resets.
UnityEngine.Random's functions are all static functions, so you can access them from the class itself without making an instance of it. System.Random requires you to make an instance of the class and access that whenever you need to do something random.
Answer by PeraDll · Mar 13, 2017 at 09:10 AM
instead of timer = delay; you can put timer = Random.Range(0, 2) (change this 2 values for different results)
Your answer
Follow this Question
Related Questions
Pause for random amount of time at waypoint 1 Answer
end game condition 1 Answer
How to make objects appear randomly around a spot 2 Answers
Survival Timer 0 Answers