Hello!
I’m pretty to this so bear with me. I started following the text adventure tutorial as I’d like to make one as a bit of a hobby project. I’ve got it working so I can print the room descriptions, click buttons to go between rooms etc, however I’m using scriptable objects for the room data and what I’ve found is if the script during runtime of the game changes any of the data, it stays that way for future runs. Which isn’t ideal. I’ve now set up a excel spreadsheet that outputs a text file which has all the room data and I’m in the process of creating a script that creates instances of the rooms from the text file.
I can get the rooms setup, however I get stuck when trying to set values to the exits. I’ve copied below the room scriptable object and the exit data class. My script gets stuck at line 74 when it tries to access the array of exits on each room returning “NullReferenceException: Object reference not set to an instance of an object.”
My first thought is I’m not creating the arrays properly on the room scriptable objects however the length of the arrays are correct as when I debug the Rooms*.exits.Length, it returns the correct length of 3 for each room. I’ve tried manually setting the text to a test string and that doesn’t work either.*
All help is welcomed!
Cheers,
Room scriptable Object
```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = “TextAdventure/Room”)]
public class Room : ScriptableObject
{
[TextArea]
public string roomName;
[TextArea]
public string description;
[TextArea]
public string returnToRoomDescription;
public Exit exits;
}*
* *Exit Data Class* *
csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Exit
{
public string exitDescription;
public Room valueRoom;
}*
* *Create Room Function* *
csharp
*public void createRooms(string textArray)
{
List Rooms = new List();
List roomTextPositions = new List();
Dictionary<string, Room> RoomNames = new Dictionary<string, Room>();
int counter = 0;
foreach (string line in textArray)
{
counter += 1;
if (line.Contains("[R]"))
{
roomTextPositions.Add(counter);
int textline = counter;
string lineHolder = line.Replace("[R]", "");
Room Room = ScriptableObject.CreateInstance("Room") as Room;
Room.roomName = lineHolder;
Room.description = textArray[counter].Replace("[D1]","");
Room.returnToRoomDescription = textArray[counter+1].Replace("[D2]", "");
Rooms.Add(Room);
RoomNames.Add(Room.roomName, Room);
}
}
for (int i = 0; i < Rooms.Count; i++)
{
//checks if the next room in text file has been found, if so will break while loop and return exits.
bool bootfromscript = false;
int Linecounter;
//make exit array per room
for (int j = 0; j < Rooms.Count; j++)
{
int roomExits = 0;
Linecounter = roomTextPositions[j];
bootfromscript = false;
while (bootfromscript != true)
{
if (textArray[Linecounter].Contains("[R]")||Linecounter==textArray.Length-1)
{
bootfromscript = true;
}
if (textArray[Linecounter].Contains("[E]"))
{
roomExits++;
Rooms[i].exits = new Exit[roomExits];
}
Linecounter++;
}
}
for (int j = 0; j < Rooms[i].exits.Length; j++)
{
Linecounter = roomTextPositions[j];
bootfromscript = false;
while (bootfromscript != true)
{
if (textArray[Linecounter].Contains("[R]") || Linecounter == textArray.Length - 1)
{
bootfromscript = true;
}
if (textArray[Linecounter].Contains("[E]"))
{
Rooms[i].exits[j].exitDescription = textArray[Linecounter+1].Replace("[ED]", "");
Rooms[i].exits[j].valueRoom = RoomNames[textArray[Linecounter].Replace("[E]", "")];
}
Linecounter++;
}
}
}
}*
```