# InstanceIDs are not unique in different scrpts.

**URL:** https://discussions.unity.com/t/instanceids-are-not-unique-in-different-scrpts/29857
**Category:** Questions & Answers
**Created:** [October 11, 2011, 1:34am UTC](https://discussions.unity.com/t/instanceids-are-not-unique-in-different-scrpts/29857 "2011-10-11T01:34:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [October 11, 2011, 1:34am UTC](https://discussions.unity.com/t/instanceids-are-not-unique-in-different-scrpts/29857/1 "2011-10-11T01:34:16Z")

</div>

I want a selected clone prefab to delete individually by GetInstanceID from another script. But GetInstanceID assign difference values by 8 to one prefab in two scripts as follows.

```
public class ControlSample : MonoBehaviour {
public static int targetID;
void OnMouseDown () {
if (Input.GetMouseButton(0)) {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit)) {
            transform.name = hit.transform.name;
            targetID = GetInstanceID();
        }
    }
}
void OnMouseDrag () {
    Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
    transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}}

```

The above script is attached to a prefab and can select and move the clone prefab with mouse.

```
public class SelectSample : MonoBehaviour {
public Transform target;
Transform clone;
ArrayList array;
void Start () {
    array = new ArrayList();
}
void Update () {
    if(Input.GetKeyDown("delete")){
        for(int i = 0; i < array.Count; i++){
            Transform clone = array *as Transform;*

```

_if(clone.GetInstanceID() == ControlSample.targetID + 8){ // Why +8?_  
_Destroy(clone.gameObject);_  
_array.Remove(clone);_  
_}_  
_}_  
_}_  
_}_  
_void OnGUI () {_  
_if(GUI.Button (new Rect (100,100,100,20), “Target”)){_  
_Transform temp = (Transform)Instantiate(target, new Vector3(0,0,0), Quaternion.identity);_  
_array.Add(temp);_  
_}_  
_}}_  
_The above script is attached to an empty gameobject and can destroy the selected clone prefab by hitting “Delete” key._  
_It works fine. But why are the assigned InstanceIDs different by 8?_

---

<div class="post-metadata">

### Author: ![Bunny83](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Bunny83](https://discussions.unity.com/u/Bunny83)
#### Post date: [October 11, 2011, 3:09am UTC](https://discussions.unity.com/t/instanceids-are-not-unique-in-different-scrpts/29857/2 "2011-10-11T03:09:24Z")

</div>

In your first script you used `this.GetInstanceID` which will return the instance ID of your script component (ControlSample) in your second script you use `clone.GetInstanceID` where clone is a Transform component so the InstanceID will be different. You can’t and shouldn’t rely on the fact that the instance ID’s of your script and the corresponding transform are different by +8.

You should either compare the InstanceIDs of the transform components OR your script components but don’t mix them.

Btw: You can also compare two Transform or two script references simply by using `==`. Both are reference types so it automatically compares the references.
