Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GameObject is null and I'm not sure why

Discussion in 'Scripting' started by Arayvenn, May 4, 2021.

  1. Arayvenn

    Arayvenn

    Joined:
    Dec 14, 2014
    Posts:
    1
    Hello there, I'm trying to get a simple highlight function working that will instantiate an arrow above an object my player is interacting with. I have code that fires when my player collides with the object which is working, my player sees a prompt to interact with objects and can interact with them when nearby, but the Highlight() function I call isn't working.

    Code (CSharp):
    1.  void Update()
    2.     {
    3.      
    4.  
    5.         // If the player presses the interact button while a prompt is diplayed, call the interact action on the object we're interacting with
    6.         if (interactPrompt.enabled == true && Input.GetKeyDown("space"))
    7.         {
    8.             action.Interact(player);
    9.         }
    10.     }
    11.  
    12.     public void OnTriggerEnter(Collider other)
    13.     {
    14.         // When the player collides with an interactable object, display the interact prompt and get the object's interactable script
    15.         if (other.gameObject.layer == 7)
    16.         {
    17.             interactPrompt.enabled = true;
    18.             action = other.GetComponent<Interactable>();
    19.             highlightController.Highlight(other.gameObject);
    20.         }
    21.     }
    I get a NullReferenceException when I collide with the object pointing to line 19 in the above block that seems to suggest other.gameObject doesn't exist. How could this be, since the if condition references this gameObject and executes properly? I assume I am missing some syntax problem.

    Here is my HighlightController class with the Highlight() function.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HighlightController : MonoBehaviour
    6. {
    7.     [SerializeField] GameObject highlighter;
    8.     GameObject currentTarget;
    9.  
    10.     // Creating our highlight arrow above our object to be highlighted
    11.     public void Highlight(GameObject highlightTarget)
    12.     {
    13.         // No need to run the rest of the code unless we are highlighting a new target.
    14.         if (currentTarget == highlightTarget)
    15.         {
    16.             return;
    17.         }
    18.         currentTarget = highlightTarget;
    19.         highlighter = Instantiate(highlighter, new Vector3(currentTarget.transform.position.x, currentTarget.transform.position.y, currentTarget.transform.position.z), highlighter.transform.rotation);
    20.     }
    21.  
    22.     // Function to disable the highlight and reset our currentTarget.
    23.     public void Hide()
    24.     {
    25.         currentTarget = null;
    26.         Destroy(highlighter);
    27.     }
    28. }
    29.  
    Any help is appreciated!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,826
    You probably made a mistake and your
    highlightController
    isn't set.
     
    StarManta likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Remember, regardless of what you are doing, the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    Schneider21 likes this.
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    just put to in toyour Start() function in script where you call it.
    Code (CSharp):
    1. HighlightController highlightController= FindObjectOfType<HighlightController>();
    and attach your HighLightedController to any gameobject in scene;

    You call script what is probably not initialized.
    Code (CSharp):
    1. highlightController.Highlight(other.gameObject);
     
    Last edited: May 4, 2021