NullRefrenceException... Even though debug shows a refrence.

Transform of an object received by a raycast, not accepted by function demanding a transform of an object.

Debug.Log(worldInteraction.lastInteractionObject.transform);
        cameraController.SetMainTarget(worldInteraction.lastInteractionObject.transform);

This line of code returns the following in the debugger:

NPCInstance (UnityEngine.Transform)
UnityEngine.Debug:Log (object)
DisableInputOnConversation:ConversationStart () (at Assets/DisableInputOnConversation.cs:42)
DialogueEditor.ConversationManager:StartConversation (DialogueEditor.NPCConversation) (at Assets/Imports/DialogueEditor/Assets/Scripts/UI/ConversationManager.cs:151)
NPC:Interact () (at Assets/Scripts/WorldInteraction/NPC.cs:12)
WorldInteraction:GetCenterObject () (at Assets/Scripts/WorldInteraction.cs:33)
WorldInteraction:Update () (at Assets/Scripts/WorldInteraction.cs:17)
NullReferenceException: Object reference not set to an instance of an object
DisableInputOnConversation.ConversationStart () (at Assets/DisableInputOnConversation.cs:43)
DialogueEditor.ConversationManager.StartConversation (DialogueEditor.NPCConversation conversation) (at Assets/Imports/DialogueEditor/Assets/Scripts/UI/ConversationManager.cs:151)
NPC.Interact () (at Assets/Scripts/WorldInteraction/NPC.cs:12)
WorldInteraction.GetCenterObject () (at Assets/Scripts/WorldInteraction.cs:33)
WorldInteraction.Update () (at Assets/Scripts/WorldInteraction.cs:17)

The refrence is set in the following block:

public class WorldInteraction : MonoBehaviour {
public GameObject lastInteractionObject;
public GameObject GetCenterObject() {
        Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, interactionRange)) {

            GameObject interactedObject = hit.collider.gameObject;
          
            if (interactedObject.tag == "Interactable") {
                lastInteractionObject = interactedObject;
                interactedObject.GetComponent<Interactable>().Interact();

            } else {
                // Debug.Log("Non-Interactable Object");
            }

            return interactedObject;
        } else {
            // Debug.Log("Error: Raycast hit nothing.");
            return null;
        }
      
    }
}

the function that requires the Transform.

public void SetMainTarget(Transform newTarget)

As you can see, it indeed returns an instance of an object. Yet it doesn’t? I can’t figure out why the function doesn’t accept that as an instance.

Thank you in advance.

Have you considered that it might be cameraController that’s null?

Sometimes you just need a fresh pair of eyes. That was indeed the case.
Thank you very much.

This three-step process works ALWAYS, and it saves you a lot of time posting irrelevant sheets of code.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null <--------- you MUST start here, no exceptions
  • Identify why it is null
  • Fix that