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 /
  • Help Room /
avatar image
0
Question by dean904 · Mar 19, 2016 at 03:16 AM · object-reference-error

Object reference not set to an instance of an object

I keep getting this error after I expanded my spawning class.

NullReferenceException: Object reference not set to an instance of an object Spawner.SpawnEnemy () (at Assets/Scripts/Spawner.cs:23) SpawnController.startWave () (at Assets/Scripts/SpawnController.cs:47) SpawnController.waveController () (at Assets/Scripts/SpawnController.cs:29)

Bellow you will find my Enemy Spawn and SpawnManager classes. Any help would be appreciated, this is driving me bonkers.

using UnityEngine; using System.Collections;

public class Spawner : MonoBehaviour {

 public GameObject Enemy;
 GameObject[] objs;
 int top;

 void Start()
 {
     objs = GameObject.FindGameObjectsWithTag("Spawn_Point");
     top = objs.Length;
  //      InvokeRepeating("SpawnEnemy", 0.01f, 5.0f);
 }

 void Update()
 {
 }

 public void SpawnEnemy()
 {
     Instantiate(Enemy, RandomCircle(objs[Random.Range(0, top)].GetComponent<Transform>().position, 5), Quaternion.identity);
     SpawnController.numAlive++;
 }

 Vector3 RandomCircle(Vector3 center, float radius)
 {
     float ang = Random.value * 360;
     Vector3 pos;
     pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
     pos.y = center.y;
     pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
     return pos;
 }

}

 using UnityEngine;
 using System.Collections;
 
 public class SpawnController : Spawner {
 
     public static int numAlive;
     int round;
     bool active;
 
     // Use this for initialization
     void Start () {
         numAlive = 0;
         round = 0;
         active = false;
         InvokeRepeating("waveController", 0.01f, 15.0f);
     }
     
     // Update is called once per frame
     void Update ()
     {
     }
 
     void waveController()
     {
         if(!active)
         {
             Debug.Log("Active now = true");
             active = true;
             startWave();
         }
         else
         {
             if(numAlive == 0)
             {
                 Debug.Log(" End round ");
                 active = false;
                 round++;
             }
         }
     }



   
 using UnityEngine;
 using System.Collections;
 
 namespace SpawningFramework
 {
     public class Enemy : MonoBehaviour
     {
 
         public float MaxHealth;
         float health;
         Vector3 pos;
 
 
         [HideInInspector]
         public bool alive;
 
         void Update()
         {
         }
 
         /// Called when this enemy has been spawned
         void Start()
         {
             health = MaxHealth;
             alive = true;
             pos = transform.position;
             
             InvokeRepeating("move", 0.01f, .2f);
 
         }
 
         void move()
         {
            // transform.Translate(0, 0.025f, 0);
         }
 
         /// Called when the player loses
         public virtual void OnGameOver()
         {
         }
 
         void OnTriggerEnter(Collider other)
         {
             Debug.Log(other.tag);
             if (other.tag == ("Player"))
             {
                 Hurt(54);
 //              Destroy(other.gameObject);        
             }
         }
 
         #region Taking Damage
         /// Hurts the enemy and returns if it dies or not
         public virtual bool Hurt(float damage)
         {
             if (!alive)
                 return false;//exit if dead
 
             health -= damage;//hurt the enemy
 
             if (health < 0.1)
             {
                 alive = false;
                 SpawnController.numAlive--;
                 Destroy(this.gameObject);
                 Destroy(this);
                 return true;
             }
 
             return health < 0.1;//return if this will die
         }
 
 
         #endregion
     }
 }
 
 
     void startWave()
     {
         Debug.Log("Starting wave: " + round);
         for(int i = 0; i < (8 + 2 * round); i++)
         {
             SpawnEnemy();
         }
     }
 }
 



Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ali-hatem · Mar 19, 2016 at 09:47 AM

in class Spawner :

 SpawnController scontroler;
 void Start()
 {
 scontroler =  GetComponent<SpawnController > ();
 }
 public void SpawnEnemy()
 {
 scontroler.numAlive++; 
 }

the same for class Enemy .

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Using Rect[] to create multiple rectangles results in error 1 Answer

How do I access the collider of a child object through a script on the another object? 0 Answers

Please Help!!! I am new to Unity I want to create a GameObject through Scripting and add sprite to it......I have done that but there is an error "Object reference not set to an instance of an object" PLS HELP!!! ASAP!!! 0 Answers

Unknown Cause of NullReferenceException? 1 Answer

SendMessage/Object Reference error 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