- Home /
My script adds +1 to variable only one time
Main script:
using UnityEngine;
using System.Collections;
public class levelScript : MonoBehaviour
{
public static int kills = 0;
}
second script when HP is equal to 0
using UnityEngine;
using System.Collections;
public class destroyEnemy : MonoBehaviour
{
void Update()
{
StartCoroutine (death());
}
IEnumerator death()
{
yield return new WaitForSeconds(6);
levelScript.kills =+ 1;
Destroy (gameObject);
}
}
When you kill the first opponent script adds +1 to the variable kills but another dead body add nothing to it. What's wrong?
In addition to @fafase's right answer, there are a couple of other things to consider. First, you are calling StartCoroutine() in Update(). That means you will be launching a new coroutine each frame. If your app was running at 60 fps, you would stack up 360 coroutines before the first one is finished. You only want to call it once.
Second, there is an optional second parameter to Destory() that gives a time in the future for the Destroy(). You can replace all of your class guts with:
void Start() {
Destroy(gameObject, 6f);
}
Answer by fafase · Sep 29, 2014 at 05:34 PM
It is += and not =+. Also, this will start a new YouTube each frame, add a boolean to prevent.
Your answer
Follow this Question
Related Questions
What am I doing wrong with PlayerPrefs? 3 Answers
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Guitext for score over time. 2 Answers
Multiple Cars not working 1 Answer