- Home /
Basic toggle timer Help!
Ok.. So i want the variable "myVariable" to randomize a number from 1, through 3 so that the possible numbers are 1, 2, and 3. BUT i wanna to generate a random number EVERY SECOND... so whats the SIMPLEST way to do this???
thanks, Gabriel
Answer by syclamoth · Jan 24, 2012 at 09:46 AM
The simplest way to do this is with InvokeRepeating and a memeber variable integer 'number'. In JS it goes like this-
var currentValue : int = -1;
var minValue : int = 1;
var maxValue : int = 3;
var randomisationInterval : float = 1;
function Start()
{
InvokeRepeating("Randomise", 0, randomisationInterval);
}
function Randomise()
{
currentValue = Random.Range(minValue, maxValue + 1);
}
Then, you just access 'currentValue'.
Answer by BiG · Jan 24, 2012 at 09:42 AM
var lock = false;
var myVariable = 1;
function Update(){
if !(lock)
RandomizeIt();
}
function RandomizeIt(){
lock = true;
yield WaitForSeconds(1);
myVariable = Random.Range(1,4);
lock = false;
}
Surely it'd be simpler just to use
while(true)
{
yield WaitForSeconds(1);
myVariable = Random.Range(1,4);
}
and then activate the coroutine in Start()?
That way you don't need the 'lock' member variable.
Yes, for sure. I've a (bad) aptitude to focus myself on the Update() function, for the biggest part of the logic :)
I've recently started experimenting with using Coroutines as an alternative to the Update loop. It's kind of convenient being able to arbitrarily suspend execution without freezing the game! Of course, it still doesn't work for everything.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
function Start () problem!! 3 Answers
component activate on timer 2 Answers
Need help with delaying a timer (javascript) 2 Answers
How do I start building my own game? 3 Answers