I just recently started following the Text Adventure tutorial on the unity page, however, as I start working on the examine item tutorial my code started giving a null reference error.
I’ve been re working to try and find what went wrong.
As far as I can tell it is assigned a reference to an object.
I put the trouble code in bold.
I am new to C#, I would greatly appreciate the help. =)
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> InteractiveDescriptionsInRooms = new List<string>();
[B] __[HideInInspector] public InteractableItems interactableItems;__[/B]
List<string> actionLog = new List<string>();
void Awake()
{
roomNavigation = GetComponent<RoomNavigation>();
[B]__ interactableItems = GetComponent<InteractableItems>();__[/B]
}
void Start()
{
DisplayRoomText();
DisplayLoggedText();
}
public void DisplayLoggedText()
{
string logAsText = string.Join("\n", actionLog.ToArray());
displayText.text = logAsText;
}
public void DisplayRoomText()
{
clearCollectionsForNewRoom();
unpackRoom();
string joinedInteractiveDescriptions = string.Join("\n", InteractiveDescriptionsInRooms.ToArray());
string combinedText = roomNavigation.currentRoom.description + "\n" + joinedInteractiveDescriptions;
logStringWithReturn(combinedText);
}
void unpackRoom()
{
roomNavigation.UnpackExitsInRoom();
prepareObjectsToTakeOrExamine(roomNavigation.currentRoom);
}
void prepareObjectsToTakeOrExamine(Rooms currentRoom)
{
for (int i = 0; i < currentRoom.interactableObjectsInRoom.Length; i++)
{
__[B] string descriptionNotInInventory = interactableItems.GetObjectsNotInInventory(currentRoom, i);[/B]__
if (descriptionNotInInventory != null)
{
InteractiveDescriptionsInRooms.Add(descriptionNotInInventory);
}
InteractableObject interactableInRoom = currentRoom.interactableObjectsInRoom[i];
for (int j = 0; i < 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()
{
[B]__ interactableItems.Clear();__[/B]
InteractiveDescriptionsInRooms.Clear();
roomNavigation.ClearExits();
}
public void logStringWithReturn(string stringToAdd)
{
actionLog.Add(stringToAdd + "\n");
}
// Update is called once per frame
void Update()
{
}
}
Here is the Interactable Items class
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(Rooms 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();
}
}
Thank you!