Quantcast
Channel: Questions in topic: "static variables"
Viewing all articles
Browse latest Browse all 84

Static variables not staying

$
0
0
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); } } }

Viewing all articles
Browse latest Browse all 84

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>