Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cakeby · Sep 16, 2011 at 04:13 PM · instantiatenullreferenceexception

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Sep 16, 2011 at 04:48 PM 0
Share

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()

avatar image cakeby · Sep 16, 2011 at 05:15 PM 0
Share

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.

avatar image loopyllama · Sep 16, 2011 at 08:37 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges