Create a unique ID for each gameobject ... but always it, in any case ...

Yes, GetInstanceID is a good solution… but not for me. GetInstanceID is regenerated at load (and reload) scene. I nedd an always unique ID. That never changes.

I try to explain, sorry for my bad english:

If the player destroys an object (kill) in the scene, when reload that scene, that object must not exist. To do this I created a static List to where add the IDs of objects when destroyed.

static public ListDestroyedObjectList=newList();

When the scene is reloaded, all objects with the ID in this List, are disabled at Awake.

void Awake(){
UniqueID=GetInstanceID().ToString();

foreach(stringCheckID in_GameManager.DestroyedObjectList){
if(CheckID==UniqueID){
gameObject.SetActive(false);
}
}
}
}

But if at scene reload the ID changes, obviously it is not working.

You might like to do?

1 Like

You should have your own mechanism (like GetInstanceID) that assigns a unique ID per object.
This could be extended to be done automatically during edit or build time to save you the work actually.

Then you can store those IDs in the list like your mentioned.

Just an idea :
-Attach a script to all your objects in scene that need an ID. (MyObjectId : Mono { public int Id; })
-Create an editor script :
FindAllScripts(MyObjectId);
foreach(var obj in List)
Obj.Id=MaxId++;
OR
Obj.Id = obj.transform.position.GetHashcode();

MaxId will generate an unique id for each object. Everytime you will add a new object to the scene, all other ID may change or you can only modify if of objects with ID = 0; But you must check if this id is not already used when you set a new id to an object.
GetHashCode() will generate an unique ID depending of the spawn position of your gameobject. If you move an object, this ID can change.

You can also place them into a list and use the Index of this object in this list to disable or enable it.
If you delete an object in the scene, the list will still contains a missing reference so your other objects will still have an unique id. You can continue to add new objects to this list.

UnitsManager : Mono { public List Objects; }

Tnx, I will try . I solved ( almost ) with an editor button (and SetDirty function) to generate a new random ID. But with each new instance I have to click the button …
Now I try your solution.