- Home /
How to destroy a gameobject on collision and keep it dead on reload of the scene?
Okay, so basically, I have a level that contains many coins, and the player can collect the coins. There are portals that can take the user to the next level, past levels, and the current level again. If the user leaves the scene to enter a new level, and a portal takes them back to a level in which they had collected coins, how do I make it so that those already-collected coins will not load in the scene?
Answer by finlay_morrison · Apr 18, 2018 at 03:40 PM
If you want it to save just between scenes, i recommend that you make a class with 'DontDestroyOnLoad(gameObject)' in the start method, then you can have a dictionary of all the coins and whether they have been collected or not and when you load a scene you make each coin check whether they are in the dictionary or not, and if they are not the add themselves to it, and if they are already on the list they check to see if they have been collected or not (a simple bool in the dictionary can handle this) and if they are collected they deactivate themselves.
If you want to be able to exit the game you are going to need to make a class that you serialize and store on the machine the user is using, this is too complex for me to type here, but i will link you to a very informative tutorial on how to serialize data and save and load it on command.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161030im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is this rendering weirdly 1 Answer
OnTriggerEnter not working, tried everything! :( (C#) 3 Answers
Can I use one script to toggle between scenes? 1 Answer
How can I have several scenes running at the same time? 1 Answer
Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers