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 List<string> DestroyedObjectList = new List<string>();
When the scene is reloaded, all objects with the ID in this List, are disabled at Awake.
void Awake () {
UniqueID=GetInstanceID().ToString();
foreach (string CheckID 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?
Answer by WILEz1975 · Dec 21, 2015 at 12:57 PM
I "semi-solved" with a custom button in inspector to generate a random id... but now i have this strange problem:
Perhaps you forgot to save the state of the object?
http://docs.unity3d.com/ScriptReference/SerializedObject.Apply$$anonymous$$odifiedProperties.html
It may be... but I do not know this function. How and where to use Apply$$anonymous$$odifiedProperties() ?
Solved with this simply line:
EditorUtility.SetDirty(myscript);
docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
Answer by Statement · Dec 20, 2015 at 11:32 PM
One solution would be to add a component to the game object whose sole purpose is to remember an ID.
public class GameID : MonoBehaviour
{
public int ID;
}
Of course, you'd have to set the ID yourself, or create an editor utility script that goes through all GameIDs in a scene and makes sure they have a unique ID each. Obviously the latter would be more convenient but requires you to write an editor extension to deal with it.