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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

NullReferenceException error

Discussion in 'Scripting' started by Shadowboi, Apr 28, 2020.

  1. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9
    Hey guys, I'm having some issues with my UI script. I get the NullReferenceException: Object reference not set to an instance of an object. Any help to try to fix this issue would be greatly appreciated. I'm using Unity 2019.2 btw

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6. public class Interaction : MonoBehaviour
    7. {
    8.     public Image uiCrosshair;
    9.     private GameObject raycastedObject;
    10.  
    11.     [SerializeField] private int rayLength = 10;
    12.     [SerializeField] private LayerMask layerInteractable;
    13.    
    14.  
    15.    
    16.     void Update()
    17.     {
    18.         RaycastHit hit;
    19.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    20.  
    21.         if (Physics.Raycast(transform.position, fwd, out hit, rayLength, layerInteractable.value))
    22.         {
    23.            
    24.             if (hit.collider.CompareTag("Object"))
    25.             {
    26.                 raycastedObject = hit.collider.gameObject;
    27.                 CrosshairActive();
    28.  
    29.                 if (Input.GetKeyDown("e"))
    30.                 {
    31.                     Debug.Log("I have interacted with an object");
    32.                 }
    33.             }
    34.         }
    35.         else
    36.         {
    37.             CrosshairNormal();
    38.         }
    39.     }
    40.  
    41.     void CrosshairActive()
    42.     {
    43.         uiCrosshair.tintColor = Color.red;
    44.     }
    45.  
    46.     void CrosshairNormal()
    47.     {
    48.         uiCrosshair.tintColor = Color.white;     //This is the culprit line that is causing the error
    49.     }
    50. }
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Did you set uiCrosshair in the editor?
     
  3. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9
    I can't. For some reason, it does not appear in the editor for me to set. This one is a real head scratcher for me. I had initially set the uiCrosshair variable as a [SerializeField] private variable and nothing appeared in the editor for me to set it. So I then changed it to a public variable and still nothing.
     
  4. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Do you have compiler errors in your console? I bet you do, since you're using UI classes, but don't have the UnityEngine.UI namespace.
     
    Joe-Censored likes this.
  6. Sauler

    Sauler

    Joined:
    Mar 27, 2020
    Posts:
    1
    You are using Image from UnityEngine.UIElements instead of Image from UnityEngine.UI
     
  7. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9
    I had a feeling it had something to do with that. But I'm not able to use the UnityEngine.UI namespace for some reason. UIElements is the only thing that comes up when I try to type in using UnityEngine.UI.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You may have picked a project template that doesn't have the UnityEngine.UI package preinstalled. Open Window-> Package Manager, select All Packages in the top left, find Unity UI, and install it. Now you should be able to use the UnityEngine.UI namespace.
     
  9. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9

    I thought so too initially. But Unity UI package is in fact installed! From what I can see so far, it's a problem I'm having with ver 2019.2 and 2019.3. If I use 2018.4, I can use UnityEngine.UI without any problems.
     
  10. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Strange. Try deleting the Library folder and restarting Unity? Might have somehow gotten the project folder corrupted.
     
  12. Shadowboi

    Shadowboi

    Joined:
    Jan 26, 2020
    Posts:
    9
    Hey I think I sort of solved the problem. I updated Unity to the latest version and updated the visual studio editor package to 1.2. I can now use the .UI namespace, however it doesn't show up as one of the prompts in the visual studio editor. None of the prompts associated with the namespace shows up either but they work nonetheless.

    Thanks for all the help tho! I appreciate it