Finding Prefab of gameobject/gameObject uniqueIds

2 questions

  1. do gameobject has unique ids associated with them i could use? something other then gameObject.name as that appears to not be unique if i instantiate a gameobject. (though im guessing i could change it)

  2. if i have a game object is there a way to discover what prefab was used to instantiate that object?

Question 1: Yes, you can use “gameObject.GetInstanceID”.

From the Unity Script Reference:

Object.GetInstanceID
function GetInstanceID () : int

Description

Returns the instance id of the object.

The instance id of an object is always guaranteed to be unique.

Question 2: Yes. If you have a variable that holds that prefab readily for instantiating, you can just refer to that variable.

Sorry to jump in, but I’m curious about this too as I’ve been using GUIDs for identifying objects.

At what point is the object ID (returned by GetInstanceID) generated? Is it created when the object is initially added to the scene hierarchy (through editor or instantiation) and after that always remains the same? Or is it regenerated every time the game is run? Or… other?

Moredice,

thanks for the info. my eyes glazed over that function. sorry my bad.

for question2 i was hoping for something built in. but yeah i came to my conclusion myself.

thank you again!

geppetto: The instance ID is uniquely generated every time for each object that is added to the hierarchy (either by instantiate or pre-placement), and yes, the ID changes every time the game runs. If you need a pre-set unique ID, you’re better off making a public variable in a script and set its ID manually, for any other script to fetch and use.

hdxpete: You’re welcome!

Thanks Moredice - I’ll stick with what I’ve already written with GUIDs then.