Quantcast
Channel: Questions in topic: "dontdestroyonload"
Viewing all articles
Browse latest Browse all 416

Calling variable within DontDestroyOnLoad() script yields different result from calling from another script without

$
0
0
I have two scripts, GameMaster and ItemCollector. **GameMaster script:** public class GameMaster : MonoBehaviour { private static GameMaster instance; [SerializeField] public int maxCherries; [SerializeField] public bool[] cherriesCollected = new bool[100]; public int numberOfCherriesCollected; public GameObject[] cherries = new GameObject[100]; private void Start() { cherries = GameObject.FindGameObjectsWithTag("Cherry"); } private void Awake() { Debug.Log(instance); if(instance == null){ instance = this; DontDestroyOnLoad(instance); } else{ Destroy(gameObject); } Debug.Log(numberOfCherriesCollected); } } **ItemCollector script:** public class ItemCollector : MonoBehaviour { private GameMaster gameMaster; private void Start() { gameMaster = GameObject.FindGameObjectWithTag("Game Master").GetComponent(); Debug.Log(gameMaster.numberOfCherriesCollected); } private void OnTriggerEnter2D(Collider2D collision) { if(collision.gameObject.CompareTag("Cherry")) { int cherryId = collision.GetComponent().id; gameMaster.cherriesCollected[cherryId] = true; gameMaster.numberOfCherriesCollected++; Debug.Log("collected cherry at: " + cherryId); for(int i = 0;i

Viewing all articles
Browse latest Browse all 416

Trending Articles



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