- Home /
Instantiating a prefabs from an array of prefabs gives a NullReferenceException error
I'm trying to create an array of prefabs from my prefabs folder so that whenever a new enemy is added (and there will be a lot, so I don't want to have to add each one in the inspector manually), I can instantiate it from the array. In my script, I'm attempting to instantiate random enemies from the array, but instead, it gives me a NullReferenceException error, telling me that the object reference is not set to an instance of an object. How do I get it to use to random prefabs selected in the array for the object reference ? Here's the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public GameObject ActiveEnemy, LeftSupportEnemy, RightSupportEnemy, InactiveEnemy; //the four positions of the enemies I'm trying to spawn
public GameObject[] allLeps; //the gameobject array
public int totalLeps = 0; //an int to keep track of the array size
public GameObject EAttackPos, ELeftSupPos, ERightSupPos, EInactivePos; //use for their transform.positions later, these are set in the inspector
private void Start()
{
allLeps = Resources.LoadAll<GameObject>("Prefabs") as GameObject[]; //this seems to be working properly since I can see the array getting populated by prefabs in the inspector
foreach (GameObject leps in allLeps)
{
totalLeps++; //this number goes up to the expected amount
}
}
public void SetRandomOpponents() //this gets called in a seperate BattleManager script
{
Debug.Log("Starting to set opponents"); //for testing purposes. It doesn't even show up, the nullreferenceexception happens first.
int E1 = Random.Range(0, totalLeps);
int E2 = Random.Range(0, totalLeps);
int E3 = Random.Range(0, totalLeps);
int E4 = Random.Range(0, totalLeps); //getting a random position in the array
GameObject ActiveEnemy = Instantiate(allLeps[E1]) as GameObject;
GameObject LeftSupportEnemy = Instantiate(allLeps[E2]) as GameObject;
GameObject RightSupportEnemy = Instantiate(allLeps[E3]) as GameObject;
GameObject InactiveEnemy = Instantiate(allLeps[E4]) as GameObject; //trying to make it so that it instantiates a random gameobject from the array, but they aren't being set
ActiveEnemy.transform.position = new Vector2(EAttackPos.transform.position.x, EAttackPos.transform.position.y);
LeftSupportEnemy.transform.position = new Vector2(ELeftSupPos.transform.position.x, ELeftSupPos.transform.position.y);
RightSupportEnemy.transform.position = new Vector2(ERightSupPos.transform.position.x, ERightSupPos.transform.position.y);
InactiveEnemy.transform.position = new Vector2(EInactivePos.transform.position.x, EInactivePos.transform.position.y); //this is just to give their their proper positions
}
}
please post Battle$$anonymous$$anager script, if the nullreferenceexeption happens first, then that code may be fine...
Answer by Ady_M · Jan 06, 2019 at 03:50 AM
Try this:
Debug.Log (string.Format ("E1-E4: ({0} , {1} , {2} , {3}) -- Max Index: ({4})", E1, E2, E3, E4, totalLeps - 1));
// Output example. Look for numbers greater than Max Index. Those are bad.
// It's over 9000!!!
// E1-E4: (0 , 9001 , 3 , 1) -- Max Index: (3)
And this:
for (int i = 0; i < allLeps.Length; i++)
{
Debug.Log (string.Format ("Lep Reference #{0} is {1}", i, allLeps[i] != null ? "OK" : "Null"));
}
// Output example
// Lep Reference #0 is OK
// Lep Reference #1 is Null
// Lep Reference #2 is OK
// Lep Reference #3 is OK
Your answer

Follow this Question
Related Questions
Array values changing after gameObject is instantiated from prefab 1 Answer
Obtaining the Variable of a Prefab in an array. 1 Answer
Adding a prefab to GameObject[] via script 1 Answer
Why does each instance of my Prefab increase drawcalls? 1 Answer
Activating a prefab in the scene by find it over a tag? 1 Answer