- Home /
Can different variables referencing the same object return different instance IDs?
I'm trying to setup a simple waypoint system for directing a NavMeshAgent along a predetermined route (e.g., on patrol).
Each instance of my Waypoint prefab has a Collider and a public continueTo property that references another Waypoint GameObject (set in the Editor).
Each NavMeshAgent has a Navigator component with a destination property that references a GameObject. When Navigator.setDestination() is called, it updates the destination property to the GameObject passed to it, and passes the new destination.transform.position to the NavMeshAgent. (Navigator's destination is a GameObject, whereas NavMeshAgent's destination is a position.)
When the Waypoint's collider is triggered, it checks the triggering GameObject 1) for a Navigator component and then if found 2) it compares its own GetInstanceID() to the Navigator's destination.GetInstanceID(). This comparison is to make sure it only responds to Navigators that have this specific Waypoint set as the destination. Since the Navigator's destination is a GameObject reference, calling GetInstanceID() from within the Waypoint's collision event and calling it on the Navigator's destination should both be getting the instance ID of the same GameObject.
This may or may not be the best way of setting up a Waypoint system, but that's not really my question. The thing that has me baffled is... The code appears to be returning two different instance ID numbers depending what variable I use to reference the destination Waypoint. If I compare instance IDs it doesn't work, but if I switch to comparing object names, it does. So it certainly appears to be referencing the same GameObject. However, I thought instance IDs were the correct way to uniquely identify an object. Names are not guaranteed to be unique, so I'd prefer not to rely on them.
Why am I getting two different instance IDs here? Is it possible for the same GameObject to return different instance IDs depending what variable you reference it from (which sounds wrong), or am I doing something wrong here (I hope)?
Navigator.cs:
using UnityEngine;
using System.Collections;
public class Navigator : MonoBehaviour {
public GameObject destination;
public bool movingTarget;
NavMeshAgent agent;
void Start() {
agent = GetComponent<NavMeshAgent>();
setDestination(destination);
}
void Update() {
if (movingTarget) {
setDestination(destination);
}
}
public void setDestination(GameObject go) {
destination = go;
agent.destination = destination.transform.position;
Waypoint wp = destination.GetComponent<Waypoint>();
if (wp.continueTo != null) {
agent.autoBraking = false;
}
else {
agent.autoBraking = true;
}
}
}
Waypoint.cs:
using UnityEngine;
using System.Collections;
public class Waypoint : MonoBehaviour {
public GameObject continueTo;
void OnTriggerEnter(Collider other) {
Debug.Log("Waypoint " + GetInstanceID() + " triggered!");
if (continueTo != null) {
Navigator nav = other.GetComponent<Navigator>();
Debug.Log("Navigator " + nav.GetInstanceID() + " detected by " + GetInstanceID() + "!");
Debug.Log("Navigator destination is " + nav.destination.GetInstanceID() + ".");
if (nav != null) {
Debug.Log("Navigator destination " + nav.destination.name + ", waypoint " + name + ".");
// if (nav.destination.name == name) {
if (nav.destination.GetInstanceID() == GetInstanceID()) {
Debug.Log("Navigator destination " + nav.destination.GetInstanceID() + " MATCHES waypoint " + GetInstanceID() + ", so we're sending it to " + continueTo.GetInstanceID() + "!");
nav.setDestination(continueTo);
}
else {
Debug.Log("Navigator destination " + nav.destination.GetInstanceID() + " DOES NOT match waypoint " + GetInstanceID() + ", so we're not doing a damned thing.");
}
}
}
Debug.Log("------------------------------------------------");
}
}
Sample debug output:
Waypoint -3636 triggered!
UnityEngine.Debug:Log(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/_BD/Scripts/Waypoint.cs:8)
Navigator -1584 detected by -3636!
UnityEngine.Debug:Log(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/_BD/Scripts/Waypoint.cs:11)
Navigator destination is 8768.
UnityEngine.Debug:Log(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/_BD/Scripts/Waypoint.cs:12)
Navigator destination Waypoint CW1, waypoint Waypoint CW1.
UnityEngine.Debug:Log(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/_BD/Scripts/Waypoint.cs:14)
Navigator destination 8768 DOES NOT match waypoint -3636, so we're not doing a damned thing.
UnityEngine.Debug:Log(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/_BD/Scripts/Waypoint.cs:21)
------------------------------------------------
UnityEngine.Debug:Log(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/_BD/Scripts/Waypoint.cs:25)