I can't seem to "attach" an instance of a scriptable object to another scriptable object

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 :frowning:

as a matter of fact…

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Ok thanks, I’m doing my best here, but I don’t think I’m attaching the Room object to the room navigation script properly. Any advice on that? I’m not sure how to do it. Usually I’d drag the first room onto the script in the inspector, but since its an instance I don’t know how to do this via code

Excellent!

You’re adding it to a list… in your mind does that constitute “attached?” It is a reasonably-done reference.

But you HAVE TO FIX THE NULLREF first.

I would change the above to be:

Room Room = ScriptableObject.CreateInstance<Room>();

but the form you had probably works, it is just subject to typos.

But again, you HAVE TO FIX THE NULLREF first.

I added it to the list as a way to keep track of all the rooms and then cycle through them in my for loops changing the required variables in the scriptable object. The part I’m struggling with is getting the Current Room to be the instantiated object. If I take out line 89 in the quoted code. I don’t receive the null reference exception.

I also made your recommended change above. Should I be using something like getcomponent somehow to “attach” my instances for both the exit arrays and the rooms to the navigation script?

Navigation Script

At runtime the game controller is attached via code, but I can’t get the room to be set as an instance created above

Anyone else have some advice? really struggling to solve this error. Cheers