Search Unity

GetComponent not Valid in Context?

Discussion in 'Scripting' started by ryand-unity, Sep 30, 2019.

  1. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    547
    I'm pretty sure I wrote this right but I'm not sure why it's not working it's for an AI it's supposed to look for an array on a separate script of connected points for which the AI are supposed to follow I'm trying to use git component to grab script with the array on it to search for points closest to the one at searching on and find the closest to move towards it.

    Code (CSharp):
    1.  
    2. internal GameObject PickNextWaypoint(GameObject currentWaypoint)
    3.     {
    4.        var best = currentWaypoint;
    5.        var bestDot = -10.0;
    6.        var cur = currentWaypoint.GetComponent(AINavPoint).Sconnected;
    7.         for (int i = 0; i < cur.Length; i++)
    8.         {
    9.             var direction = Vector3.Normalize(cur[i].transform.position - transform.position);
    10.             var dot = Vector3.Dot(direction, Dir[Random.Range(0, Dir.Length)]);
    11.             if (dot > bestDot && cur[i] != currentWaypoint)
    12.             {
    13.                 bestDot = dot;
    14.                 best = cur[i];
    15.             }
    16.     }
    17.  
    18.     return best;
    19.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The syntax is GetComponent<AINavPoint>()
     
    Joe-Censored likes this.
  3. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    547
    LOL WOW this is been bugging me for a couple days I thought I had already tried that but that works awesome thank you