Hello,
I’m writing a text adventure at the moment and I’m coding the method that takes an array of strings and turns them into scriptable objects. I did this so I can create the objects at runtime and I’m not worried about changing any of their values permanently. I’ve got two issues:
- Is there a way to view the scriptable objects instances I create? I think this would help in debugging my problem
- I seem to have to created the right objects, my code just gets stuck at line 89 in the code below because of the error below. Note I’ve only copied the method that creates the scriptable objects and line 258 is line 89.
NullReferenceException: Object reference not set to an instance of an object
GameController.createRooms (System.String[ ] textArray) (at Assets/Scripts/GameController.cs:258)
GameController.Awake () (at Assets/Scripts/GameController.cs:32)
public void createRooms(string[] textArray)
{
List<Room> Rooms = new List<Room>();
List<int> roomTextPositions = new List<int>();
Dictionary<string, Room> RoomNames = new Dictionary<string, Room>();
//create room objects
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.description = textArray[counter].Replace("[D1]","");
Room.returnToRoomDescription = textArray[counter+1].Replace("[D2]", "");
Rooms.Add(Room);
RoomNames.Add(lineHolder, 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++;
}
}
//Setup exits and attach to rooms
for (int j = 0; j < Rooms[i].exits.Length; j++)
{
int Exitcounter = 0;
Linecounter = roomTextPositions[i];
bootfromscript = false;
while (bootfromscript != true)
{
if (textArray[Linecounter].Contains("[R]") || Linecounter == textArray.Length - 1)
{
bootfromscript = true;
}
if (textArray[Linecounter].Contains("[E]"))
{
Exit exit = new Exit();
exit.exitDescription = textArray[Linecounter + 1].Replace("[ED]", "");
exit.valueRoom = RoomNames[textArray[Linecounter].Replace("[E]", "")];
Rooms[i].exits[Exitcounter] = exit;
Exitcounter++;
}
Linecounter++;
}
}
}
roomNavigation.currentRoom = Rooms[0];
}
Any ideas? I’m stuck ![]()