Need Help Fixing NullReferenceException in Text Adventure Game
Hello!
I have been following the Text Adventure Game Tutorial on the Unity Help page (here is the first video), specifically this video, towards the end. When it came time to test my game, I got this error:
NullReferenceException: Object reference not set to an instance of an object GameController.ClearCollectionsForNewRoom () (at Assets/Scripts/GameController.cs:122) GameController.DisplayRoomText () (at Assets/Scripts/GameController.cs:48) GameController.Start () (at Assets/Scripts/GameController.cs:29)
This is the script that seems to be causing the issue:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public Text displayText;
public InputAction[] inputActions;
[HideInInspector] public RoomNavigation roomNavigation;
[HideInInspector] public List<string> interactionDescriptionsInRoom = new List<string>();
[HideInInspector] public InteractableItems interactableItems;
List<string> actionLog = new List<string>();
void Awake () {
roomNavigation = GetComponent<RoomNavigation>();
interactableItems = GetComponent<InteractableItems>();
}
void Start()
{
DisplayRoomText();
DisplayLoggedText();
}
public void DisplayLoggedText ()
{
string logAsText = string.Join("\n", actionLog.ToArray());
displayText.text = logAsText;
}
public void DisplayRoomText ()
{
ClearCollectionsForNewRoom();
UnpackRoom();
string joinedInteractionDescriptions = string.Join("\n", interactionDescriptionsInRoom.ToArray());
string combinedText = roomNavigation.currentRoom.description + "\n" + joinedInteractionDescriptions;
LogStringWithReturn(combinedText);
}
void UnpackRoom ()
{
roomNavigation.UnpackExitsInRoom();
PrepareObjectsToTakeOrExamine(roomNavigation.currentRoom);
}
void PrepareObjectsToTakeOrExamine (Room currentRoom)
{
for (int i = 0; i < currentRoom.interactableObjectsInRoom.Length; i++)
{
string descriptionNotInInventory = interactableItems.GetObjectsNotInInventory(currentRoom, i);
if (descriptionNotInInventory != null)
{
interactionDescriptionsInRoom.Add(descriptionNotInInventory);
}
InteractableObject interactableInRoom = currentRoom.interactableObjectsInRoom[i];
for (int j = 0; j < interactableInRoom.interactions.Length; j++)
{
Interaction interaction = interactableInRoom.interactions[j];
if (interaction.inputAction.keyWord == "examine")
{
interactableItems.examineDictionary.Add(interactableInRoom.noun, interaction.textResponse);
}
}
}
}
public string TestVerbDictionaryWithNoun (Dictionary<string, string> verbDictionary, string verb, string noun)
{
if (verbDictionary.ContainsKey(noun))
{
return verbDictionary[noun];
}
return "You cannot " + verb + " " + noun;
}
void ClearCollectionsForNewRoom()
{
interactableItems.ClearCollections();
interactionDescriptionsInRoom.Clear();
roomNavigation.ClearExits();
}
public void LogStringWithReturn (string stringToAdd)
{
actionLog.Add(stringToAdd + "\n");
}
void Update () {
}
}
And this is the script that is referenced at line 122:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractableItems : MonoBehaviour {
public Dictionary<string, string> examineDictionary = new Dictionary<string, string>();
[HideInInspector] public List<string> nounsInRoom = new List<string>();
List<string> nounsInInventory = new List<string>();
public string GetObjectsNotInInventory (Room currentRoom, int i)
{
InteractableObject interactableInRoom = currentRoom.interactableObjectsInRoom[i];
if (!nounsInInventory.Contains(interactableInRoom.noun))
{
nounsInRoom.Add(interactableInRoom.noun);
return interactableInRoom.description;
}
return null;
}
public void ClearCollections()
{
examineDictionary.Clear();
nounsInRoom.Clear() ;
}
}
I have a sneaking suspicion that this is a very simple and easy problem to fix (knock on wood), but I have been searching for several hours and have not been able to find anything. Many thanks in advance for any help and/or guidance you can provide. I am happy to provide any more information about my project that might help in solving this problem.
Your answer
Follow this Question
Related Questions
Null Reference Exception Error 1 Answer
Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer
ArgumentOutOfRange: Argument is out of range Parameter name: index 0 Answers
NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer
Weird Error With References 1 Answer