I'm creating a script so a player knows when they collect 5 coins in a row. For some reason, if there is too much time between coin collection Unity resets my variable back to 0...and I'm not sure what is causing this.
public class coin : MonoBehaviour {
static int coinsCaughtInSequence = 0;
void Start(){
coinsCaughtInSequence = 0;
}
void OnTriggerEnter2D(Collider2D collider){
if(collider.tag == "Player"){
coinsCaughtInSequence++;
if(coinsCaughtInSequence >= 5){
Debug.Log ("5 coins collected");
}
Debug.Log(coinsCaughtInSequence);
Destroy(this.gameObject);
}
}
}
↧