x


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

more ▼

asked Sep 16, 2011 at 04:13 PM

cakeby gravatar image

cakeby
1 6 4 6

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 coming from the EnemyGoblin script somewhere inside of IsDead()

Sep 16, 2011 at 04:48 PM syclamoth

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.

Sep 16, 2011 at 05:15 PM cakeby

EnemyGoblin must derive from MonoBehaviour to have Start() called automatically. What class does it derive from?

Sep 16, 2011 at 08:37 PM loopyllama
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Sep 29, 2011 at 02:26 PM

cakeby gravatar image

cakeby
1 6 4 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2969
x789

asked: Sep 16, 2011 at 04:13 PM

Seen: 1349 times

Last Updated: Sep 29, 2011 at 02:26 PM