x


Increasing a random InvokeRepeating

I'm making a game where the player character moves toward the right constantly. i have obstacles and coins being spawned in the players path using

InvokeRepeating(spawn,0,random.range(#,#)).

the problem is that it picks a number and wont let me change that number during the game. i thought if i put it as "InvokeRepeating(spawn,0,(random.range(#,#) + spawnRate)" it would change when i changed the spawnRate.

Is there a way to do this without rewriting the whole thing?

more ▼

asked Mar 20 '12 at 01:39 AM

MrRstar gravatar image

MrRstar
20 6 6 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I assume by # you mean an actual number, and by actual number, I mean a floating-point number. As long as those two numbers are different, it should pick a new number each time. Assuming this is in an Update function, not Start or Awake. Which is what I'm guessing you did.

Replace InvokeRepeating in Start with Invoke in Update, putting your random value there.

Actually, you probably should use a 'timer'. Something like:

var timeBetween : float;
var lastTime = 0.0;

function Start()
{
  timeBetween = Random.value;
}

function Update()
{
  // check to see if random time has elapsed
  if ((Time.time - lastTime) < timeBetween)
  {
    // it has, get a new random time
    lastTime = Time.time;
    timeBetween = Random.Range (1.0, 2.0); // for example
    // spawn the item
    Spawn();
  }
}
more ▼

answered Mar 20 '12 at 02:16 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x573
x440
x50

asked: Mar 20 '12 at 01:39 AM

Seen: 591 times

Last Updated: Mar 20 '12 at 02:21 AM