Search Unity

NullReferenceException error

Discussion in 'Scripting' started by Dapphidx, Jun 4, 2019.

  1. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
    Hey guys I kinda need some help....
    It gives me error at the first one on line 13
    and at the second one at line 29
    Please help me I don't know how to resolve this


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NPC : Interactable {
    6.  
    7.     public string[] dialogue;
    8.  
    9.     public string npcName;
    10.    
    11.         public override void Interact()
    12.     {
    13.         DialogueSystem.Instance.AddNewDialogue(dialogue, npcName);
    14.        
    15.         Debug.Log("Interacting with NPC.");
    16.  
    17.     }
    18. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class Interactable : MonoBehaviour
    7.  
    8. {
    9.     [HideInInspector]
    10.     public NavMeshAgent playerAgent;
    11.     private bool hasInteracted;
    12.    
    13.     public virtual void MoveToInteraction(NavMeshAgent playerAgent)
    14.     {
    15.         hasInteracted = false;
    16.         this.playerAgent = playerAgent;
    17.         playerAgent.stoppingDistance = 3f;
    18.         playerAgent.destination = transform.position;
    19.  
    20.      
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         if (playerAgent != null && !playerAgent.pathPending)
    26.         {
    27.             if (!hasInteracted && playerAgent.remainingDistance <= playerAgent.stoppingDistance)
    28.             {
    29.                 Interact();
    30.                 hasInteracted = true;
    31.             }
    32.         }
    33.     }
    34.  
    35.     public virtual void Interact()
    36.     {
    37.         Debug.Log("Interacting with base class.");
    38.     }
    39. }







    It gives me error at the first one on line 13
    and at the second one at line 29
    please help me I don't know how to resolve this
     
    Last edited: Jun 4, 2019
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Use code tags, we're not gonna count lines to find line 13 or line 29. Also you included several scripts, so indicate which of the scripts those errors refer to as well.
     
  3. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
    I have problems at both of them .... and is it ok like that??? Can you help me now?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Right, but surely it's not line 13 on both files and line 29 on both files?
     
  5. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
    No .. It's 13 on the first one and 29 on the second one
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  7. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    OK, so on the first one, DialogueSystem.Instance is clearly null. Is there a DialogueSystem in the scene somewhere, and is it on an object that's active? Otherwise, can you show the code for DialogueSystem?
     
  9. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
    I can show the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DialogueSystem : MonoBehaviour
    7. {
    8.    
    9.     public static DialogueSystem Instance { get; set; }
    10.     public GameObject dialoguePannel;
    11.     public string npcName;
    12.     public List<string> dialogueLines = new List<string>();
    13.  
    14.     Button continueButton;
    15.     Text dialogueText, nameText;
    16.     int dialogueIndex;
    17.  
    18.     void Awake()
    19.     {
    20.         continueButton = dialoguePannel.transform.Find("Continue").GetComponent<Button>();
    21.         dialogueText = dialoguePannel.transform.Find("text").GetComponent<Text>();
    22.         nameText = dialoguePannel.transform.Find("Name").GetChild(0).GetComponent<Text>();
    23.         continueButton.onClick.AddListener(delegate { ContinueDialogue(); });
    24.         dialoguePannel.SetActive(false);
    25.  
    26.         if(Instance != null && Instance != this)
    27.         {
    28.             Destroy(gameObject);
    29.         }
    30.         else
    31.         {
    32.             Instance = this;
    33.         }
    34.        
    35.     }
    36.  
    37.     public void AddNewDialogue(string[] lines, string npcName)
    38.     {
    39.         dialogueIndex = 0;
    40.         dialogueLines = new List<string>(lines.Length);
    41.         dialogueLines.AddRange(lines);
    42.         this.npcName = npcName;
    43.         CreateDialogue();
    44.        
    45.  
    46.     }
    47.     public void CreateDialogue()
    48.     {
    49.         dialogueText.text = dialogueLines[dialogueIndex];
    50.         nameText.text = npcName;
    51.         dialoguePannel.SetActive(true);
    52.     }
    53.  
    54.  
    55.     public void ContinueDialogue()
    56.     {
    57.         if (dialogueIndex < dialogueLines.Count - 1)
    58.         {
    59.             dialogueIndex++;
    60.             dialogueText.text = dialogueLines[dialogueIndex];
    61.         }
    62.         else
    63.         {
    64.             dialoguePannel.SetActive(false);
    65.         }
    66.  
    67.     }
    68.    
    69. }
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'd start by adding a Debug.Log("here") line next to line 32 of DialogueSystem, to ensure that that line is being executed (and that it's happening before your other script is running).
     
  11. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
    I did it then what do i do?
     
  12. Dapphidx

    Dapphidx

    Joined:
    Jun 4, 2019
    Posts:
    9
    Now it gives me an error at the 22nd line from the dialogue system