- Home /
Don't change the variable value every time you load script
Hey there !
I'm new to unity and i'm trying to load scenes based on items collected. Problem is that the counter is not counting my acquired items. I'm using OnTriggerEnter2D()
to trigger the event; Below is the snippet:
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Player")) { collectionNumber += 1; Destroy(gameObject); if (collectionNumber == 1) { collision.gameObject.transform.Find("Acquired_Items").GetChild(0).gameObject.SetActive(true); qiCollector.gameObject.transform.GetChild(0).gameObject.SetActive(true); } else if (collectionNumber == 2) { collision.gameObject.transform.Find("Acquired_Items").GetChild(1).gameObject.SetActive(true); qiCollector.gameObject.transform.GetChild(1).gameObject.SetActive(true); } else if (collectionNumber == 3) { collision.gameObject.transform.Find("Acquired_Items").GetChild(2).gameObject.SetActive(true); qiCollector.gameObject.transform.GetChild(2).gameObject.SetActive(true); } else { Debug.LogWarning("All Items Collected !!"); } cN.text = "Collection Number " + collectionNumber.ToString(); } }
Whenever a new scene is loaded this script is loaded because it is on my quest item. And for every scene there is a quest item. So what I want to do is basically keep track of my collectionNumber
, but it resets to 0
.
Any help is much appreciated :)
Answer by $$anonymous$$ · Jun 20, 2019 at 09:41 AM
I think you have two different approaches to achieve this:
You either write it to a file via some encoding - so players cannot modify the file easily - at the end of every scene and the start of every screen you read the file, decode it and store it in your variable. In long term you'll have to do this anyways since if someone closes your application you'll loose the variable unless you saved it somewhere outside of the program
You make a class for e.g.: Globals, declare static variables and in its Awake or Start method you call the DontDestroyOnLoad() method on the GameObject itself. This method preserves your GameObject - and the attached scripts with its variables as well - even if a new scene is loaded. For more information, visit this link: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
This is rather an old video but you can get the idea from here: https://www.youtube.com/watch?v=tW_ADGMdiko
I used Singleton for the purpose and it is working as I expected to be. Sure ! I'll try this solution in future problems like these. These Link is helpful: Singleton in Unity
Thank you for the Answer :)
That's a good approach as well, although I read it some time ago that it's not the safest method. For now, it surely looks like it was the best solution, but I'd advise you that in the future if your game gets more serious, look into coding and encoding so you can write your own save file :) Btw, if you were satisfied with what I wrote I would appreciate if you accepted my solution as an answer but only if you feel like!
Answer by killop1997 · Jun 20, 2019 at 09:37 AM
You could set the values on a game object that has dontdestroyonload() method, it could be your player or a game manager of some sort, it depends how you write your code, but if you exit the game the data will be lost. You can also save and load the data, here is a link to a good tutorial on that Preserving Data.
I used Singleton for the purpose and it is working as I expected to be. Sure I'll try this solution in future problems like these.
Thank you for the Answer :)