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.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.”);
}
}