Accessing GameObjects by InstanceID

Hi There.

I’m looking for a way to get a gameobject by it’s InstanceID.
I’ve seen this: “EditorUtility.InstanceIDToObject”, but unfortunately it only exists in the editor assembly.
But there seems no way to do that at game time?

My second thought was to store an InstanceID/Gameobject mapping by myself. But I would need some kind of events that tell me when a gameobject is instantiated or destroyed to keep that list up to date.
Are there such events?

Any other ideas?

Thanks and Greets, Daniel

Has this changed in any recent Unity updates, or is this still the only answer? It seems really, really bad that Unity cannot give you access to an instantiated object by its ID, since the instances will inherit the name and tag of the original.

–

1 Answer

1

Creating your own class that tracks the gameObjects by their instance id’s is the only way that I’m aware of doing it. Unity does not provide any way to iterate through objects in a scene by their instance id other than the mentioned editor classes. There are also no events fired that coincide with instantiation and destroying of objects inside of Unity, this will have to be created yourself as well.

It’s probably best to create a class that has your required variables such as the instance id, whether it is active or not, etc, and then create an array of classes to track yourself.