Search Unity

Refering to gameobject in other script

Discussion in 'Scripting' started by BenVenNL, Nov 27, 2019.

  1. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
  2. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    I can't test right now (because not behind pc with Unity on it).
    My guess is that the pathfindingScript remains null, because GetComponent<PathfindingScript>(); returns nothing. When you try to call pathfindingScript.nodeA or pathfindingScript.nodeB you get the null reference exception.

    You could try to fix the GetComponent call by prefacing it with gameObject. Try:
    Code (CSharp):
    1. pathfindingScript = gameObject.GetComponent<PathfindingScript>();
    2.  
    3. if (pathfindingScript != null)
    4. {
    5.    print("found it");
    6. }
    7. else
    8. {
    9.    print("no pathfindingscript found");
    10. }
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please don't cross post, suggestions have been offered in the other thread with no reply.