Search Unity

NullReferenceException despite reverting changes

Discussion in 'Editor & General Support' started by SuccessFail, Apr 15, 2021.

  1. SuccessFail

    SuccessFail

    Joined:
    Jan 14, 2021
    Posts:
    23
    I have a script here meant to open the canvas and make dialogue pop up and it has been working great till now. I started messing with this script to get individual names to pop up for my characters, but my dialogue stopped coming up.

    I decided it was better to revert all my changes, but the same "NullReferenceException: Object reference not set to an instance of an object" message kept popping up, even after changing it back to how it was when it was working.

    I've made a lot of changes and unfortunately hadn't backed up on Collab in a day, meaning I have about 6 hours of coding that I don't want to lose. If anyone can spot the issue (apparently line 41 of the second script) it would be the best. Otherwise I'm scrapping and rewriting these.

    The first script here is for the dialogue box.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DialogueManager : MonoBehaviour
    7. {
    8.     public GameObject dBox;
    9.     public Text dText;
    10.     public bool dActive;
    11.     public string[] dialogueLines;
    12.     public int currentLine;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.      
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if(dActive && Input.GetButtonDown("Back") || dActive && Input.GetButtonDown("Advance"))
    25.         {
    26.             currentLine++;
    27.         }
    28.  
    29.         if(currentLine >= dialogueLines.Length)
    30.         {
    31.             dBox.SetActive(false);
    32.             dActive = false;
    33.  
    34.             currentLine = 0;
    35.         }
    36.  
    37.         dText.text = dialogueLines[currentLine];
    38.     }
    39.  
    40.  
    41.     public void ShowDialogue()
    42.     {
    43.         dActive = true;
    44.         dBox.SetActive(true);
    45.     }
    46. }
    47.  
    This second one goes on the npc

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using UnityEngine;
    5.  
    6. public class DialogueHolder : MonoBehaviour
    7. {
    8.     public string dialogue;
    9.     private DialogueManager dMan;
    10.     public bool canTalk;
    11.     public string[] dialogueLines;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         dMan = FindObjectOfType<DialogueManager>();
    17.         canTalk = false;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (canTalk == true)
    24.         {
    25.        
    26.         }
    27.  
    28.     }
    29.  
    30.     private void OnTriggerEnter2D(Collider2D collision)
    31.     {
    32.         canTalk = true;
    33.     }
    34.  
    35.     private void OnTriggerStay2D(Collider2D other)
    36.     {
    37.         if(other.gameObject.name == "Player")
    38.         {
    39.             if (Input.GetButton("Advance"))
    40.             {
    41.                 if (dMan.dActive)
    42.                 {
    43.  
    44.                     dMan.dialogueLines = dialogueLines;
    45.                     dMan.currentLine = 0;
    46.                     dMan.ShowDialogue();
    47.  
    48.                 }
    49.             }
    50.         }
    51.     }
    52.  
    53.     private void OnTriggerExit2D(Collider2D collision)
    54.     {
    55.         canTalk = false;
    56.     }
    57. }
    58.  
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Steps to fix this null reference exception (and any other null reference exception):

    * What could be null? Inspect line 41. The only possible thing that could be null on that line is the field dMan. Double check and confirm that dMan is null. Add a Debug.Log statement just before that line, "Debug.Log(dMan)" and see what prints.
    * Why is it null? Find out how you assign something to dMan. Usually, this is either via the inspector, or in code. In your case, it is via code, specifically on line 16. Add a Debug.Log statement to verify that it is null, same as above. It would seem that FindObjectOfType is returning null.
    * Read the docs for FindObjectOfType: https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html The only way it could return null is either the object doesn't exist, or it is deactivated. Check that a component of type "DialogueManager" exists in the scene, and that it is not deactivated.
     
  3. I put these on two active game objects and after some basic modification (input stuff) they are working. You probably didn't attach the DialogueManager script to anything or the game object is disabled.
     
  4. SuccessFail

    SuccessFail

    Joined:
    Jan 14, 2021
    Posts:
    23
    Just in case anyone else comes to this, I fixed it up. Two issues, one is that when messing with the canvas for a pause menu I accidentally had turned off the a large section of the canvas, which my dialogue manager was inside. Second, is an actual coding error. In line 46 with ShowDialogue it is in a bad location, as it is trying to do everything inside dMan before it is active. Moving ShowDialogue up an if statement fixes this.

    Huge thanks to bobisgod234, and the unity community for helping me out every week or two.
     
    bobisgod234 likes this.