How to stop an item that has already been collected from respawning when player returns to a scene?

In my game I have a collectible which is in abundance and I want each individual collectible to not respawn if the player has already collected it and returns to the scene. For example, there might be a hundred or so of these collectibles in one scene and if the player collects one of them, leaves the scene and then returns, the one they have collect doesn’t respawn but the rest do.
After some research I think I’m supposed to use data serialization to store which collectibles have been collected and which haven’t but I’m unsure of how to go about doing this and the concept is quite new to me. Could someone explain to me what I’m supposed to do please?


i have the same problem

It depends if you really only just want to conserve the state between scene or between play session. (If you close the game and reopen it, do you want to have the collectible respawned ?)

For the former, you can could simply use the Object.DontDestroyOnLoad [1] function in conjonction with SceneManager.sceneLoaded[2] plus a way to uniquely identify each object. Each time you pick up an item, you add its value to a list of picked up item in a script that has been mark with DontDestroyOnLoad. (It most not be a reference to the actual collectable but it’s identifier). Whenever you load a scene, you would then deactivate/activate the collectable in function of what is in the list of DontDestroyOnLoad script.

For the latter, it is pretty much the same, except you would need to save the result in a file and you do not need the DontDestroyOnLoad/SceneManager.sceneLoaded; You can use the Awake call of any script to deactivate/activate the collectable. (Just don’t forget to save whenever you need to do so)

[1] Unity - Scripting API: Object.DontDestroyOnLoad

[2] Unity - Scripting API: SceneManagement.SceneManager.sceneLoaded

You need to active the object before to begin like this

[SerializeField] private GameObject Object;

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

And put the object in the inspector, if you want it to save how many times you grabbed the objects you have to save them every time you grab one.