Search Unity

Bug Bug in my Dynamic Crosshair Script

Discussion in 'Scripting' started by Renantasy, Jan 3, 2022.

  1. Renantasy

    Renantasy

    Joined:
    Jan 3, 2022
    Posts:
    2
    My code is giving me this error when I enter the sphere collider with trigger enabled:
    NullReferenceException: Object reference not set to an instance of an object
    DynamicCrosshair.Update () (at Assets/Scripts/DynamicCrosshair.cs:41)



    here is my two scripts, CursorDetection attached to 2 objects and DynamicCrosshair attached to player

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CursorDetection : MonoBehaviour
    7. {
    8.     public bool mouseOver;
    9.  
    10.     void OnMouseOver()
    11.     {
    12.         mouseOver = true;
    13.     }
    14.  
    15.     void OnMouseExit()
    16.     {
    17.         mouseOver = false;
    18.     }
    19. }
    20.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DynamicCrosshair : MonoBehaviour
    7. {
    8.     public CursorDetection cursorDetection;
    9.     public GameObject selectedCrosshair;
    10.     public GameObject talkCrosshair;
    11.     private bool inTrigger;
    12.     private bool isGhost;
    13.     private bool isInteract;
    14.  
    15.     void OnTriggerEnter(Collider playerCollider)
    16.     {
    17.         if (playerCollider.GetComponent<Collider>().tag == "Ghost")
    18.         {
    19.             inTrigger = true;
    20.             isGhost = true;
    21.             isInteract = false;
    22.         }
    23.  
    24.         if (playerCollider.GetComponent<Collider>().tag == "Interact")
    25.         {
    26.             inTrigger = true;
    27.             isGhost = false;
    28.             isInteract = true;
    29.         }
    30.     }
    31.  
    32.     void OnTriggerExit(Collider playerCollider)
    33.     {
    34.         inTrigger = false;
    35.         isGhost = false;
    36.         isInteract = false;
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         if (inTrigger == true && cursorDetection.mouseOver == true && isGhost == true)
    42.         {
    43.             talkCrosshair.SetActive(true);
    44.         }
    45.  
    46.         else
    47.         {
    48.             talkCrosshair.SetActive(false);
    49.         }
    50.  
    51.         if (inTrigger == true && cursorDetection.mouseOver == true && isInteract == true)
    52.         {
    53.             selectedCrosshair.SetActive(true);
    54.         }
    55.  
    56.         else
    57.         {
    58.             selectedCrosshair.SetActive(false);
    59.         }
    60.     }
    61. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You probably need to assign the CursorDetection reference in your inspector view.
     
    D12294 likes this.
  3. Renantasy

    Renantasy

    Joined:
    Jan 3, 2022
    Posts:
    2
    already do that
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Rather than each of us guess what you might have forgotten to do, you can fix it yourself this way.

    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.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    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.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032