Instantiate giving a NullReferenceError

Let me start by saying I’m new to Unity. I have a class that calls the SpawnFloor() function from the Spawner class. Because I’m not sure where the error is, I’ve posted the whole Spawner class below.

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
	//Number of unique floors
	static int NumberOfUniqueFloors = 4;
	//List of floors
	public GameObject[] FloorsArray = new GameObject[NumberOfUniqueFloors];
	// Use this for initialization
	void Start () {
		//Add floors to list by tag
		for (int i = 0; i < FloorsArray.Length; i++) {
			FloorsArray *= GameObject.Find("Floor" + i);*
  •  }*
  • }*

  • void Update () {*

  • }*

public void SpawnFloor()

  •  //Random number based on number of floors*
  •  int randomIndex = Random.Range(0, NumberOfUniqueFloors - 1);*
  •  //Instantiate a random floor*
  •  Transform theNewFloor;*
  •  theNewFloor = Instantiate(FloorsArray[randomIndex], new Vector3(0, 0, 0), Quaternion.identity) as Transform;*
  •  //Debug purposes*
  •  Debug.Log("SpawnFloor called");*

The game runs fine until it goes to call the SpawnFloor() function in which case I get thousands of the following error:
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82)
Spawner.SpawnFloor () (at Assets/Game 3 - Marble game/Scripts/Spawner.cs:31)
ScrollUp.Update () (at Assets/Game 3 - Marble game/Scripts/ScrollUp.cs:30)
This is my first time using Instantiate so I believe that’s where I’m going wrong. I’ve tried quite a few suggestions I found on the internet and all produce the same error. Any help is greatly appreciated and may prevent any more internal bleeding from the slamming of my head on the desk. :slight_smile:

In this code…

 for (int i = 0; i < FloorsArray.Length; i++) {         
   FloorsArray *= GameObject.Find("Floor" + i);*

Add a call to Debug.Log and make sure it’s actually finding gameobjects named Floor0, Floor1, Floor2, etc.
I assume you actually have gameobjects that have been given those names?