Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vivredraco · Jan 31, 2016 at 02:21 PM · navmeshagentobject referenceinstancesidwaypoint system

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)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I fix this error 0 Answers

Variables related to instance 0 Answers

Nullreference Exception with ScriptableObject 0 Answers

Is there an impossible value for Object.GetInstanceID() 0 Answers

Using navmesh as controller and making character jump ( unable to jump as of now) 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges