InstanceIDs are not unique in different scrpts.

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?

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.