- Home /
Instantiated object gives NullReferenceException
Hi,
We have an event system that spawns Goblins via a GoblinManager like this:
using UnityEngine;
using System.Collections;
public class EventGoblinWave : EventT {
private GoblinManager mManager;
public EventGoblinWave(GoblinManager aManager) : base("Goblin Wave")
{
mManager = aManager;
//spawn 3 goblins here
for (int i = 0; i < 3; i++)
{
mManager.SpawnGoblin(new Vector3(5, 47, -7));
}
}
public void Update()
{
base.Update();
}
}
now we want to spawn some from an object that is on the scene via the same GoblinManager and we do it like this:
using UnityEngine;
using System.Collections;
public class GoblinHut : MonoBehaviour
{
public int HitPoints;
private GoblinManager mManager;
private MainGameLoop mMainGameLoop;
private float mSpawnTimer;
// Use this for initialization
void Start () {
HitPoints = 200;
mSpawnTimer = -1;
mMainGameLoop = GameObject.Find("MainGameLoop").GetComponent("MainGameLoop") as MainGameLoop;
mManager = mMainGameLoop.GetGoblinManager();
}
// Update is called once per frame
void Update ()
{
if (mManager == null)
{
mManager = mMainGameLoop.GetGoblinManager();
}
if (mSpawnTimer >= 2)
{
mManager.SpawnGoblin(transform.position);
mSpawnTimer = 0;
}
else
{
mSpawnTimer += Time.deltaTime;
}
}
public void GetHit(int aHitPointLoss)
{
if (HitPoints > 0)
{
HitPoints -= aHitPointLoss;
}
}
}
when we spawn the object from the GoblinHut via the GoblinManager we get a NullReferenceException when the GoblinManager check if the goblin spawned is dead. It seems it's because the Start() has not been called when called from the GoblinHut. The thing I don't understand is that Start() is called when the event spawn them.
Here is the GoblinManager class:
using UnityEngine;
using System.Collections;
public class GoblinManager : MonoBehaviour {
private ArrayList mGoblins;
public GameObject GoblinPrefab;
private Object mMainLoop;
public void SetMainLoop(Object aMainLoop)
{
mMainLoop = aMainLoop;
}
// Use this for initialization
void Start ()
{
mGoblins = new ArrayList();
}
public void SpawnGoblin(Vector3 aPos)
{
GameObject newGoblin = Instantiate(GoblinPrefab, aPos, Quaternion.identity) as GameObject;
newGoblin.active = true;
if (newGoblin != null)
{
mGoblins.Add(newGoblin);
}
}
// Update is called once per frame
public void Update ()
{
for (int i = 0; i < mGoblins.Count; i++)
{
GameObject obj = (GameObject)mGoblins[i];
if (obj.active)
{
EnemyGoblin goblin = obj.GetComponent<EnemyGoblin>();
if (goblin != null)
{
if (goblin.IsDead())
{
Destroy((Object)mGoblins[i]);
mGoblins.RemoveAt(i);
break;
}
}
}
}
}
}
Thanks for you help
Have a good look at your stack trace- what lines, exactly, are causing the error? From the looks of things, the null reference exception is co$$anonymous$$g from the EnemyGoblin script somewhere inside of IsDead()
Yeah. Inside of IsDead we have return (mBaseCharacter.HitPoints <= 0);. mBaseCharacter is instantiated in Start() of EnemyGoblin. Since the Start() is not called we get this error.
EnemyGoblin must derive from $$anonymous$$onoBehaviour to have Start() called automatically. What class does it derive from?
Answer by cakeby · Sep 29, 2011 at 02:26 PM
Finally, I am getting this error because my loop is trying to access it before it is really created by Unity. I tried to used the "active" property but it is already set to true.
Your answer

Follow this Question
Related Questions
Simultaneous Null Reference Exception and expected value 1 Answer
Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers
Accessing a prefab after instantiating results as null 1 Answer
Unity iOS Javascript - Problem with Instantiate 1 Answer
Networking-multplayer issue 1 Answer