Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique unchanged identifier for gameobject in unity

I have to store information about every object (gameobject/mesh) in an excel file. Like I have a sphere object I am getting its instanceId and saving it in excel. Within the excel file along with object instance id i am saving additional things like object display name, type.

  objInstanceId = transform.GetInstanceID();

My question is that is the safest way? I checked that instanceID is unique but I am not sure if it is gets changed ever? Like if I replace my model will it get changed? I need a unique and unchanged identifier for every single mesh so that I can save it in an excel file and associate some data with the identifier!

like image 736
Muhammad Faizan Khan Avatar asked Sep 15 '25 21:09

Muhammad Faizan Khan


1 Answers

Let's take a look at the docs:

The instance ID of an object is always unique. The ID changes between player runtime and Editor sessions. As such, the ID is not reliable for performing actions between the Editor and runtime sessions, for example, loading an object state from a save file.

https://docs.unity3d.com/2021.1/Documentation/ScriptReference/Object.GetInstanceID.html

So within a runtime session the id is guaranteed to be unique. Between sessions the ids may change.

In other words, exporting all instance ids, adding/changing values and then importing them again will work within a runtime session (when the app is running).

It won't work when you close the app and start another runtime session, in which case any created objects will have different ids.

like image 106
sommmen Avatar answered Sep 17 '25 09:09

sommmen