Search Unity

How to get GameObject of current NavMeshLink from NavMeshAgent?

Discussion in 'Navigation' started by gardnej6, Nov 1, 2017.

  1. gardnej6

    gardnej6

    Joined:
    Mar 9, 2017
    Posts:
    1
    With the the old OffMeshLink and NavMeshAgent you could get the GameObject the current OffMeshLink was attatched to by doing...
    upload_2017-11-1_18-30-28.png

    However with the new NavMeshLink, this doesn't work. agent.isOnOffMeshLink still works fine but the link.offMeshLink returns as null. Anyone have an idea what to do?
     
    sanmn19 and AntonioModer like this.
  2. MiFrilke

    MiFrilke

    Joined:
    Dec 14, 2016
    Posts:
    41
    Code (CSharp):
    1.  
    2. if(agent.isOnOffMeshLink)
    3. {
    4.     OffMeshLinkData data = agent.currentOffMeshLinkData;
    5.          
    6.     if(data.offMeshLink)
    7.     {
    8.         Debug.Log("on OFFmeshLink: " + data.offMeshLink.name, data.offMeshLink.gameObject);
    9.     }
    10.     else
    11.     {
    12.         Object owner = agent.navMeshOwner;
    13.         Debug.Log("on NAVmeshLink: " + owner.name, (owner as Component).gameObject);
    14.     }
    15. }
    The built in NavMeshLink sets itself as owner on the agent when it is being traversed, so thats how you would get information about it. You can cast the owner object to a component (or NavMeshLink specifically) to get access to the gameobject it is attached to.