- Home /
Static variable to check objects between scene reloads
Basically I have some items on the game which the player can pick up. When those items are picked they disappear. I also want it so that if the player dies, when the scene is reloaded, those items that he already picked up, won't spawn again.
Standard procedure was to create a static List, which I add a picked object, and when the scene reloads the List will keep the information.
My problem:
1st run - player picks up Object A. Object A is added to the list.
2nd run - when the scene starts the Object A checks if the list already contains himself, but it always returns false.
Basically Object A in Scene X on run 1, is different from Object A in Scene X on run 2. Is there any way that I can relate those two objects?
I tried with gameObject, with a MonoBehavior, with GetInstanceId(), but none of those worked.
Answer by HarshadK · Sep 18, 2014 at 11:24 AM
You can assign a unique ID to all your pickup objects in a script that you can write and attach to all your pickup objects. It is just a variable that you assign to your each game object with its value as the ID for that object. Be sure to provide each instance of an object a unique ID so that no two instances of same type of object has the same ID (in case required). Now when the object is serialized this ID will be serialized as the ID of that object.
Now when the object is picked up you can store that ID in your static list. Whenever the scene loads again you can check if that ID is present in the list. Since these IDs are unique and are stored in the object itself in your script.
Yeah, but I really didn't want to do that because there's a lot of items and a lot of levels. I wanted something that wouldn't require manual work.
You can create an editor script to generate and assign these IDs to game objects for you.
Answer by VamshiKrishnaP · Sep 18, 2014 at 11:58 AM
when you are playing 2nd time, values never reset until application quit. Don't assign any values to static variables in start() / awake() . just declare the static variables.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
object.ToString never equals the string 2 Answers
An object reference is required to access non-static member `UnityEngine.Component.transform' 1 Answer
Object rotation from web page values 1 Answer
Do you have to set a object reference for every script? 0 Answers
Enable/Disable a Static object 1 Answer