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?