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 Actnshj200 · Nov 27, 2017 at 06:14 AM · unity 5script.nullreferenceexceptionspawning

Null Reference for Spawning Character

I cant figure out whats wrong with this code I keep getting a null reference exception error but my player is referenced correctly maybe y'all can find what is wrong with this code.And he is dropped inside the component used to instantiate it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerSpawner : MonoBehaviour {
 
     public enum SpawnState { Spawning, Waiting, Counting };
 
     [System.Serializable]
     public class Wave
     {
         public string name;
       
         public int count;
         public float rate;
     }
     private healthBar health;
     public Wave[] waves;
     private int nextWave = 0;
     public int Recorder = 0;
     public float timeBetweenWaves = 5f;
     public float waveCountdown;
     private float searchCountdown = 1f;
     public GameObject[] spawnPoints;
     public GameObject enemy;
     public SpawnState state = SpawnState.Counting;
 
     void Awake()
     {
         waveCountdown = timeBetweenWaves;
         health = FindObjectOfType<healthBar>();
     }
 
     // Use this for initialization
     void Start()
     {
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (state == SpawnState.Waiting)
         {
             if (!EnemyIsAlive())
             {
                 WaveCompleted();
             }
             else
             {
                 return;
             }
         }
 
 
         if (waveCountdown <= 0)
         {
             if (state != SpawnState.Spawning && !GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
             {
 
                 
                    StartCoroutine(SpawnWave(waves[nextWave]));
                 
                
             }
         }
         else
         {
             waveCountdown -= Time.deltaTime;
         }
     }
 
     bool EnemyIsAlive()
     {
         searchCountdown -= Time.deltaTime;
         if (searchCountdown <= 0f)
         {
             searchCountdown = 1f;
 
             if (GameObject.FindGameObjectWithTag("Player") == null)
             {
                 return false;
             }
         }
 
         return true;
     }
 
     void WaveCompleted()
     {
         state = SpawnState.Counting;
         waveCountdown = timeBetweenWaves;
 
         if (nextWave + 1 > waves.Length - 1)
         {
             nextWave = 0;
             print("All waves Complete");
 
         }
 
         
 
         else
         {
             nextWave++;
         }
 
     }
 
 
 
     IEnumerator SpawnWave(Wave _wave)
     {
 
         state = SpawnState.Spawning;
         for (int i = 0; i < _wave.count; i++)
         {
             SpawnEnemy();
             Recorder= _wave.count;
             yield return new WaitForSeconds(1f / _wave.rate);
         }
 
         if(_wave.count == 3)
         {
             _wave.count = 1;
         }
         state = SpawnState.Waiting;
 
         yield break;
 
     }
 
     void SpawnEnemy()
     {
       
 
       
 
         //GameObject _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
         Instantiate(enemy, transform.position, transform.rotation);
         //Instantiate(Resources.Load("US_Ranger"), transform.position, transform.rotation);
     }
 }

This is where the error starts. alt text

spawn-error2.png (28.4 kB)
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 jchester07 · Nov 27, 2017 at 07:38 AM 0
Share

FindGameObjectWithTag returns only "active" gameobjects and as I understand, the player is inactive at that point of your script which unity can't find that gameobject then returns null.

You might want to get a reference of your player at the start of your script and use that variable throughout the script ins$$anonymous$$d of using GameObject.FindGameObjectWithTag("Player")

avatar image Actnshj200 jchester07 · Nov 27, 2017 at 07:47 PM 0
Share

I think that where the confusion sets for me. Because it will return false. To where it will instantiate a new character to where it will find the player again. Functionally I can't find out the solution to why that line of code doesn't work.

avatar image jchester07 Actnshj200 · Nov 28, 2017 at 12:32 AM 0
Share

If you want to instantiate a new character, you might want to use a prefab and get a reference of that instantiated prefab.

0 Replies

· Add your reply
  • Sort: 

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

148 People are following this question.

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

Related Questions

Destroy(this.gameObject) destroys all the obejcts in the scene, i want only the one that is clicked on 1 Answer

How to stop movement script on void start and resume after. 0 Answers

Client Spawning Player Objects 0 Answers

Why is my destroy platform script now working? NullReferenceException: Object reference not set to an instance of an object. 1 Answer

Rotation on camera not working in webGL 0 Answers


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