- Home /
Question by
MrJamieMcC · May 24, 2017 at 07:40 AM ·
invokeinvokerepeatingcancelinvoke
Issue with starting/canceling invokes
Im having an issue with canceling/starting invokes in my update. I have a time "frenzyTimer" which sets a bool in another class in which will state what type of invoke should be running. The start invoke works fine. The first if statement in update works fine also. Its just the second if statement that doesnt work. The debugs are being called but the "FrenzySpawn" invoke is never cancelled and the "SpawnBall" invoke is never started. I even check to see if the invoke is not running, as isaid. The debugs are being called correctly. Help please. Thank you
public GameObject defaultBall,lifeBall,pointsBall,frenzyBall;
int invokeCount;
int spawnLocation;
float xPos;
float frenzyTimer;
int lifeBallChance;
int pointsBallChance;
int frenzyBallChance;
private void Start()
{
InvokeRepeating("SpawnBall", 0, 6); //Run the SpawnBall method every 6 seconds
invokeCount += 1; //Counts how many balls have been spawned
}
private void Update()
{
if (Ball.frenzyActive == true && !IsInvoking("FrenzySpawn")) //if frenzyActive is true and "FrenzySpawn" is not running
{
CancelInvoke("SpawnBall");
InvokeRepeating("FrenzySpawn", 0, 1);
Debug.Log("frenzy is active");
}
if (Ball.frenzyActive == false && !IsInvoking("SpawnBall")) //if frenzyActive is false and "SpawnBall" is not running
{
CancelInvoke("FrenzySpawn");
frenzyTimer = 0;
InvokeRepeating("SpawnBall", 0, 6);
Debug.Log("frenzy has been turned off");
}
Debug.Log("Frenzy time = " + frenzyTimer);
}
public void SpawnBall()
{
Debug.Log("Started spawn ball method");
lifeBallChance = Random.Range(1, 100);
pointsBallChance = Random.Range(1,100);
frenzyBallChance = Random.Range(1, 100);
if (lifeBallChance >= 90)
{
xPos = Random.Range(-8f, 8f);
Instantiate(lifeBall, new Vector3(xPos, 12, 14), Quaternion.identity);
}
else if (lifeBallChance <90 && pointsBallChance >= 80)
{
xPos = Random.Range(-8f, 8f);
Instantiate(pointsBall, new Vector3(xPos, 12, 14), Quaternion.identity);
}
else if (lifeBallChance <90 && pointsBallChance <80 && frenzyBallChance >95)
{
//spawn frenzy
}
else
{
xPos = Random.Range(-8f, 8f); //Gets a random X axis range between -8 & 8
Instantiate(defaultBall, new Vector3(xPos, 12, 14), Quaternion.identity); // spawns ball at xpos,12,-16 with default rotation
}
}
public void FrenzySpawn()
{
frenzyTimer += 1;
lifeBallChance = Random.Range(1, 100);
pointsBallChance = Random.Range(1, 100);
if (frenzyTimer <= 10)
{
if (lifeBallChance >= 90)
{
xPos = Random.Range(-8f, 8f);
Instantiate(lifeBall, new Vector3(xPos, 12, 14), Quaternion.identity);
}
else if (lifeBallChance < 90 && pointsBallChance >= 80)
{
xPos = Random.Range(-8f, 8f);
Instantiate(pointsBall, new Vector3(xPos, 12, 14), Quaternion.identity);
}
else
{
xPos = Random.Range(-8f, 8f); //Gets a random X axis range between -8 & 8
Instantiate(defaultBall, new Vector3(xPos, 12, 14), Quaternion.identity); // spawns ball at xpos,12,-16 with default rotation
}
}
if (frenzyTimer >10)
{
Ball.frenzyActive = false;
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612122524im_/https://answers.unity.com/themes/thub/images/avi.jpg)