- Home /
Instantiate random from a list gives error: "Reference not set to an instance"
Hello there. I'm having a problem. I'm new writing here, so I will try to be clear with this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectsSpawnController: MonoBehaviour {
private GameEngine mEngine;
List<GameObject> prefabList = new List<GameObject>();
public GameObject bullet1;
public GameObject bullet2;
public GameObject bullet3;
public GameObject bullet4;
private float InstantiationTimer = 2f;
public float InstantiatonFinal;
Vector3[] positions;
Vector3 pos1;
Vector3 pos2;
Vector3 pos3;
Vector3 pos4;
int prefabNum;
// Use this for initialization
public ObjectsSpawnController(GameEngine engine)
{
mEngine = engine;
}
void Start()
{
prefabList.Add(bullet1);
prefabList.Add(bullet2);
prefabList.Add(bullet3);
prefabList.Add(bullet4);
prefabNum = UnityEngine.Random.Range(0, 3);
positions = new Vector3[] {new Vector3(0f, 1.2f, 12.39f), new Vector3 (0f, 1.2f, -12.39f),
new Vector3(19f, 1.2f, 0f), new Vector3(-19f, 1.2f, 0f)};
pos1 = positions[Random.Range(0, positions.Length)];
pos2 = positions[Random.Range(0, positions.Length)];
pos3 = positions[Random.Range(0, positions.Length)];
pos4 = positions[Random.Range(0, positions.Length)];
}
// Update is called once per frame
public void Update () {
CreatePrefab();
}
void CreatePrefab()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0)
{
var bullets = Instantiate(prefabList[prefabNum] , pos1, Quaternion.identity);
bullets.transform.parent = gameObject.transform;
InstantiationTimer = InstantiatonFinal;
}
}
}
What I'm trying to do is to randomly instantiate some objects to four places so in any of the 4 places could appear any of the bullets I'm trying to instantiate. The problem is in the line : var bullets = Instantiate(prefabList[prefabNum] , pos1, Quaternion.identity);
in prefabList[prefabNum] where in console gives me the alert of: NullReferenceException: Object reference not set to an instance of an object ObjectsSpawnController.CreatePrefab () (at Assets/Scripts/ObjectsSpawnController.cs:69) ObjectsSpawnController.Update () (at Assets/Scripts/ObjectsSpawnController.cs:59)
How should I proceed? Thanks a lot
Answer by Oliver1135 · May 18, 2018 at 04:16 PM
I dont really understand your code, its not doing what you say its doing. it's always using pos1 and randoming through your list of game objects for different kinds of bullet to spawn
I would recommend breaking the statement in order to find out what is null, the bullets might be null, the list might be null. even brute force can help pin down what might be null for you. Just explode out what you are trying to do and try to cover all cases
void CreatePrefab()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0)
{
Debug.Log("b1"+bullet1);
Debug.Log("b2"+bullet2);
Debug.Log("b3"+bullet3);
Debug.Log("b4"+bullet4);
Debug.Log("pf1"+prefabList[0]);
Debug.Log("pf2"+prefabList[1]);
Debug.Log("pf3"+prefabList[2]);
Debug.Log("pf4"+prefabList[3]);
var bullets = Instantiate(prefabList[prefabNum] , pos1, Quaternion.identity);
bullets.transform.parent = gameObject.transform;
InstantiationTimer = InstantiatonFinal;
}
}