Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object crosshair ui linked

Discussion in 'Scripting' started by Talio202, May 10, 2019.

  1. Talio202

    Talio202

    Joined:
    Aug 27, 2018
    Posts:
    3
    Hi:

    Okay so I've got my script and it's finding objects and changing the cursor colour, but I'm getting this error and I think it's because my Interactable script is messing with my NPC script which makes the NPC script think it needs a crosshair.
    Help would be seriously appreciated.

    NullReferenceException: Object reference not set to an instance of an object
    Interactable.<Update>g__CrosshairNormal|4_1 () (at Assets/Assets/Scripts/GameGrindRPG/Interactable.cs:48)
    Interactable.Update () (at Assets/Assets/Scripts/RPG/Interactable.cs:38)

    Interactables script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class Interactable : MonoBehaviour
    {
    private GameObject raycastedObj;

    [SerializeField] private int rayLength = 10;
    [SerializeField] private LayerMask layerMaskInteract;

    [SerializeField] private Image uiCrosshair;

    void Update()
    {
    RaycastHit hit;
    Vector3 fwd = transform.TransformDirection(Vector3.forward);

    if (Physics.Raycast(transform.position, fwd, out hit, rayLength, layerMaskInteract.value))
    {
    if (hit.collider.CompareTag("Interactable Object"))
    {

    raycastedObj = hit.collider.gameObject;
    CrosshairActive();


    if (Input.GetKeyDown("e"))
    {
    Debug.Log("interacted with object");
    Interact();
    }
    }
    }
    else
    {
    CrosshairNormal();
    }

    void CrosshairActive()
    {
    uiCrosshair.color = Color.red;
    }

    void CrosshairNormal()
    {
    uiCrosshair.color = Color.white;
    }

    }


    NPC script:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class NPC : Interactable
    {
    // can create individual JSON files per npc, this is a basic system instead
    public string[] dialogue;
    public string name;

    public override void Interact()
    {
    DialogueSystem.Instance.AddNewDialogue(dialogue, name);
    Debug.Log("Interacting with NPC.");
    }

    }

    public virtual void Interact()
    {
    raycastedObj.SetActive(false);
    Debug.Log("Interacting with base class.");
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,824
    Without trying to evaluate design, your UiCrosshair is null. If not everything will have a value in that field, just check if it's not null before you try to change it's color.
     
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    629
    Yeah this is just a typical debugging job. Your most common solution would be to comment out sections of your code and uncomment them one or a few lines at a time until you find the source of the problem.
     
unityunity