- Home /
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:
http://answers.unity3d.com/questions/64832/player-kill-streak.html
Please help! thank you!
Answer by woodoo · Jan 14, 2014 at 03:55 AM
They way i would do this is something like that:
Check if an enemy was killed
If so, increment the killStreak and stop the Coroutine called ResetStreak
Start the coroutine ResetStreak (It will set killStreak to 0 after 'X' seconds.
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.
your method wont work because the script that stops and starts the coroutines when an enemy dies, needs to be on the enemey. And when the enemy dies, the script becomes inactive.
Is there another method of doing this?
Answer by CipherZero · Jan 14, 2014 at 03:40 AM
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()
Answer by Gucci Gagan · Mar 10, 2014 at 01:16 PM
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!!
Your answer
Follow this Question
Related Questions
Change InvokeRepeating in another script 1 Answer
problem with shooting killing script 1 Answer
How to make a Kill Counter 1 Answer
Problem with combo attacks 2 Answers
Display var on gui not working... 2 Answers