Kill Streak Help

Hi, I was having some issues with my kill streak script.

I think InvokeRepeating would be the best option.

void Update () 
	{
        InvokeRepeating("StreakTimer", 1.0F, 1.0F);
    }

void StreakTimer()
	{	
		if(killTimer > 0)
		{
            //TODO increment when enemy dies
			tempComboNum++;
		}
		
		if (--killTimer == 0) 
 		{
            tempComboNum = 0;
			CancelInvoke ("StreakTimer");
		}

	}

I then update the gui text with the tempComboNum.

The issue

How do i increment the combo when an enemy dies?
The kill streak script is attached to main camera and theres an enemyAI script attached to each enemy.

I used this as reference:

Please help!
thank you!

you may want to avoid using InvokeRepeating() in Update(), it tends to get messed up. Instead of this, you can try to put InvokeRepeating() in somewhere that will be called once only, like Start()

They way i would do this is something like that:

  1. Check if an enemy was killed
  2. If so, increment the killStreak and stop the Coroutine called ResetStreak
  3. Start the coroutine ResetStreak (It will set killStreak to 0 after ‘X’ seconds.
  4. Loop it (just write the code inside Update()

Here’s a snippet to help you to write it:

int killStreak = 0;
int resetTime = 4;

void Update()
{
    if (!enemyKilled)
        return;

    killStreak += 1;
    StopCoroutine("ResetStreak");
    StartCoroutine(ResetStreak());
}

void ResetStreak()
{
    yield return new WaitForSeconds(resetTime);
    killStreak = 0;
}

Note that “enemyKilled” variable will be some expression you’ll write to check if you’ve killed an enemy.

Hope it helps.

I got it finally. I just had to create the coroutine in another script and start and stop it from the enemy script when the enemy dies.

I wasn’t aware you could call coroutines from other scripts.

THANK YOU!!