- Home /
InvokeRepeating affecting parts of script
Hey guys, I need some help,
I have a random enemy wave generator set up and although "InvokeRepeating" works for the swarm ships and circle ships, for some bizarre reason it doesn't work for the wave ships and the waveTimer stops after one second when the Circle function is launched.
Here is the code
#pragma strict
public var spawners : Transform[];
public var asteroid : Transform;
public var swarmShip : Transform;
public var waveShip : Transform;
public var waveShipR : Transform;
public var selection : int;
public var waveTimer : float;
public var countdown = false;
public var Timer : int;
function Start ()
{
InvokeRepeating ("Generator", 40.0f, 40.0f);
yield WaitForSeconds (10);
audio.Play();
InvokeRepeating ("Asteroid", 0.01f, 0.5f);
yield WaitForSeconds (20);
CancelInvoke("Asteroid");
}
function Update()
{
Timer = Time.time;
if ((selection == 0) || (selection == 1) || (selection == 2) && (waveTimer <= 30) && (countdown == false))
{
waveTimer -= Time.deltaTime;
countdown = true;
if (waveTimer <= 0.0)
{
countdown = false;
waveTimer = 30.0;
selection = -1;
}
}
Debug.Log ("Wave Time - " + waveTimer);
}
function Generator()
{
selection = -1;
if (selection == -1)
{
selection = Random.Range(0, spawners.length);
}
if (selection == 0)
{
audio.Play();
InvokeRepeating ("Swarm", 0.5f, 0.5f);
if (waveTimer <= 0.0)
{
CancelInvoke ("Swarm");
selection = -1;
}
}
if (selection == 1)
{
audio.Play();
Wave();
WaveR();
if (waveTimer <= 0.0)
{
selection = -1;
}
}
if (selection == 2)
{
audio.Play();
InvokeRepeating ("Circle", 5.0f, 5.0f);
if (waveTimer <= 0.0)
{
CancelInvoke ("Circle");
selection = -1;
}
}
}
function Asteroid ()
{
var vec = Vector3 (transform.position.x, Random.Range (-3.5f, 5.4f));
Instantiate(asteroid, vec, transform.rotation);
}
function Swarm ()
{
var vec = Vector3 (transform.position.x, Random.Range (-3.5f, 5.4f));
Instantiate(swarmShip, vec, transform.rotation);
}
function Wave ()
{
for (var i : int = 0; i < 5; i++)
{
Instantiate(waveShip, spawners[1].position, spawners[1].rotation);
yield WaitForSeconds (0.4);
}
}
function WaveR ()
{
for (var j : int = 0; j < 5; j++)
{
Instantiate(waveShipR, spawners[1].position, spawners[1].rotation);
yield WaitForSeconds (0.4);
}
}
function Circle ()
{
var vec = Vector3 (transform.position.x, Random.Range (-3.5f, 5.4f));
Instantiate(spawners[2], vec, transform.rotation);
}
These are the sections causing problems.
if (selection == 1)
{
audio.Play();
Wave();
WaveR();
if (waveTimer <= 0.0)
{
selection = -1;
}
}
if (selection == 2)
{
audio.Play();
InvokeRepeating ("Circle", 5.0f, 5.0f);
if (waveTimer <= 0.0)
{
CancelInvoke ("Circle");
selection = -1;
}
}
In the inspector, the selection number shows "1" and the waveTimer starts counting down, however, nothing appears when I try "InvokeRepeating ("Wave", 3.0f, 3.0f)" and "InvokeRepeating ("WaveR", 3.0f, 3.0f)". When they are set as above with just Wave() and WaveR() it works, but only instantiates one set of them and that's it.
With Circle function, the waveTimer starts and then just stops after one second, the other waves spawn fine, it just means that the circle wave keeps spawning.
And if I try to use "yield WaitForSeconds", it seems to break the Generator function.
Any ideas?
Any other ideas of how to repeat a function?
Thanks
Answer by Eric5h5 · Jun 30, 2013 at 05:17 PM
You can't use coroutines with InvokeRepeating.
Ok, I figured that much from the WaitForSeconds breaking the generator function, So why does the Swarm() function work even though it has InvokeRepeating on it's Generator function event and the InvokeRepeating doesn't for the Wave effect, and the waveTimer stops working on the circle function even though the InvokeRepeating works?
Also, that answer hasn't really answered the question.....
Ok, I managed to get the waveTimer working by splitting up the if statement in the Update function into separate parts rather then having the OR statement to test to see which selection has been made, however the original problem still remains with the Wave and waveR function not repeating.
I thought with the changes it might work, but see$$anonymous$$gly it doesn't want to repeat.
This is the code I am using
if (selection == 1)
{
audio.Play();
InvokeRepeating ("Wave", 0, 4.0f);
InvokeRepeating ("WaveR", 0, 4.0f);
}
and the check in the Update function
if ((selection == 1) && (waveTimer <= 30))
{
waveTimer -= Time.deltaTime;
if (waveTimer <= 0.0)
{
waveTimer = 30.0;
selection = -1;
CancelInvoke ("Wave");
CancelInvoke ("WaveR");
}
}
Your answer
Follow this Question
Related Questions
Wait For Seconds Instantiate Inside For Loop 2 Answers
Pause InvokeRepeating? 1 Answer
Bulletshells not working as intended 2 Answers
Invoke on Struct or some else time wait 1 Answer
InvokeRepeating doesn't work 2 Answers