Search Unity

Refering to GameObject in other script.

Discussion in 'Getting Started' started by BenVenNL, Nov 26, 2019.

  1. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    52
    I have a "GameLogic" GameObject wich has some information about certain GameObject's I'd like my actor (Actor_1) to use for pathfinding.

    On the GameLogic GameObject I have a script called 'PathfindingScript' for general usage.


    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    public class PathfindingScript : MonoBehaviour
    {
    public GameObject nodeA;
    public GameObject nodeB;


    // Lots of other stuff in here. //

    }


    I can see 'nodeA' and 'node B' in the inspector. And it's working fine.

    By pressing a key I spawn my "Actor_1" GameObject into the scene and the console says "Actor 1 is awake". So that is working! Actor_1 has the following script.


    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    public class PathCopyScript1 : MonoBehaviour
    {
    PathfindingScript pathfindingScript;
    private GameObject gameLogic;
    public GameObject nodeA;
    public GameObject nodeB;

    private int layer_mask;

    //Lots of other stuff. //

    void Awake()
    {
    print("Actor1 is Awake!");

    layer_mask = LayerMask.GetMask("Pf_Nodes_Layer");
    gameLogic = GameObject.FindGameObjectWithTag("GameLogic");
    pathfindingScript = GetComponent<PathfindingScript>();
    }
    void Update()
    {
    // Lots of stuff //

    if (Input.GetKeyDown("2"))
    {
    nodeA = pathfindingScript.nodeA;
    nodeB = pathfindingScript.nodeB;

    }

    // Lots of other stuff. //
    }


    This thing goes wrong when I hit '2'.
    Console: Object referene is not set to an instance of an object.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,180
  3. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    52
  4. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    52
    I attached the 'PathCopyScript1' to the 'Actor_1'-prefab, could that be the problem?
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    389
    This is a generic error, it's essential to know where exactly the error occurs. Therefore always post the full error including stack trace and line numbers.

    This occurs when you assume a variable contains an object reference but it actually doesn't (the variable is null). The first thing you need to figure out is which variable is null. The exact location of the error is crucial for this. Also, as Jeff pointed out, use Debug.Log to print out the variables to make sure they actually contain what you expect.
     
  6. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    52
    Well, and again it's human error.
    Did more debugging and pathfindingScript was 'Null'.

    So I changed
    pathfindingScript = GetComponent<PathfindingScript>();

    into
    pathfindingScript = gameLogic.GetComponent<PathfindingScript>();

    and voila!

    I was expecting the code to be fine, because the well trained eye of the community did not spot this. Ah well, problem solved and learned something today.
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,180
    To be clear, we did spot it, but were trying to point you in the right direction via debugging. Give vs teach!
     
  8. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    32
    Ok then... the well trained eye of the community actually did spot it. Not only that, the community spoonfed you the solution.

    See crosspost you did:
    https://forum.unity.com/threads/refering-to-gameobject-in-other-script.784196/
     
  9. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    52
  10. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    52
    Thnx for this valuable lesson:)