Version: 2022.1
LanguageEnglish
  • C#

Object.GetInstanceID

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public int GetInstanceID();

Returns

int Returns the instance ID of the object. When used to call the origin object, this method returns a positive value. When used to call the instance object, this method returns a negative value.

Description

Gets the instance ID of the object.

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.

using UnityEngine;

public class ExampleScript : MonoBehaviour { // Create 10 game objects, which will have random Instance IDs void Awake() { for (int i = 0; i < 10; i++) { GameObject go = new GameObject("abc" + i.ToString("D3")); } }

// Find all the game objects and display their Instance IDs void Start() { Object[] allObjects = Object.FindObjectsOfType<GameObject>();

foreach (GameObject go in allObjects) { Debug.Log(go + " is an active object " + go.GetInstanceID()); } } }