Search Unity

null reference exception issue

Discussion in 'Scripting' started by unity_IdF3Y7NrR21HSw, Jun 17, 2018.

  1. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    I can`t seem to figure out why do I get this Null Reference Exception, tried all I could think of but it`s still there, the code works properly though, just want to know why and how to fix this issue.

    Code (CSharp):
    1. Vector3 twoPos;
    2.  
    3. void Update()
    4. {
    5.   twoPos = GameObject.FindGameObjectWithTag("2").transform.position;
    6. }
    I just want to constantly get the position of the game object tagged 2.
    Full error line: "NullReferenceException: Object reference not set to an instance of an object"
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    It just must mean that there isn't a game object in your scene with the tag "2". Are you sure you spelled the tag correctly? The tag is literally just the number "2" in the Unity Editor? Is something creating/destroying the object at some point?

    Some related advice: FindGameObjectWithTag is fairly slow, and not the sort of thing you want to do every frame, generally. Two better approaches are:
    • In the Start method, call GameObject.FindGameObjectWithTag(), and assign that game object to a private class variable. From that point on, you can access that game object's transform.
    • Alternately, instead of using FindGameObjectWithTag, create a public GameObject property on the class, and assign the game object in the inspector.
     
  3. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    When the game starts there is no game object with the tag 2, it gets spawned later during the game, could this be the issue? Also I need to constantly update the position of the game object with the tag 2, getting the position of the object tagged 2 at the start of the game won`t help a lot because it will just give me the same position as the spawner, right?
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    That's definitely the issue. Update runs every frame. You're looking for an object, and trying to access its properties even when it doesn't exist. That's what causes NullReferenceExceptions. To avoid that, you would find the game object, and check whether it's null before trying to access its properties.

    When you get access to a gameObject, you can later access its `transform.position`. You're right that if you got its position at the start of the level that value would never change. But if you get the gameObject, and later access its `transform.position`, it will always be the current position of the object.

    So, with all that said, you could do something like this:

    Code (CSharp):
    1. private GameObject _objectTwo;
    2.  
    3. void Update() {
    4.     if (_objectTwo == null) {
    5.         _objectTwo = GameObject.FindGameObjectWithTag("2");
    6.     }
    7.  
    8.     if (_objectTwo != null) {
    9.         // Do something with _objectTwo.transform.position;
    10.     }
    11. }
    Again, calling FindGameObjectWithTag every frame isn't ideal. Ideally you'd use some other approach to informing this script that the game object exists, but it's not the end of the world to use this approach initially.
     
  5. unity_IdF3Y7NrR21HSw

    unity_IdF3Y7NrR21HSw

    Joined:
    Apr 3, 2018
    Posts:
    32
    Everything`s clear now, thank you very much kind sir, have a nice day!
    Wish they`d explain like this in the manual too.