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 unity_rQfRAZYVVnSfag · Aug 06, 2018 at 12:50 AM · instantiateinspectorenemyspawnobjects

NullReferenceException: Instatiate with construtor OR in Inspector

Hey Comm-unity,

i am currently working on a TowerDefense Game and want to implement an infinite level. I have a WaveSpawner with public HeaderVariables in which in can drag and drop Prefabs(Enemies) which will then be instatiated into the wave. These Header Variables are mostly arrays or Objects which include an Array and a number (To have different #Enemies for each run). For the infite level i added constructors to the these header variables to initialize all waves virtually and not in the inspector. This worked but however when i switch back to the normal levels I don´t get any Enemies. (I tracked the issue down to the constructor while using the Debugger but I could be wrong with that since i am very new to unity).

Maybe someone can smell what the issue is already. I am grateful for any help!

Thank you for your time. If can share my whole code if necessary.

Tim

NullReferenceException: Object reference not set to an instance of an object Wave.Startactually () (at Assets/Script/Wave.cs:24) WaveSpawner+c__Iterator0.MoveNext () (at Assets/Script/WaveSpawner.cs:116) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) WaveSpawner:Update() (at Assets/Script/WaveSpawner.cs:90)

Wave.cs

 using System.Collections; using System.Collections.Generic; using UnityEngine;
 
 [System.Serializable] public class Wave{
 
     public EnemiesPerWave[] enemies;
 
     public bool shuffleBefore;
     public int killNodes;
 
     //private GameObject[] spawnList;
     private List<GameObject> spawnList = new List<GameObject>();
     private int enemyCounter = 0;
 
     
 
     public void Startactually()
     {
         foreach(EnemiesPerWave entry in enemies){
             for(int i = 0; i<=entry.count; i++)
             {
                 spawnList.Add(entry.enemy);
                 
             }
         }
     }
 
 
     public float rate;
     public GameObject getNextEnemy()
     {
         GameObject ret =  spawnList[enemyCounter];
         enemyCounter++;
         return ret;
     }
     public int count()
     {
         // number of total enemies
         return spawnList.Count;
     }
     public Wave(float rate, bool shuffleBefore, int killNodes, EnemiesPerWave[] wave1, bool isInfinity)
     {
         if (isInfinity)
         {
             Debug.Log("const");
             this.rate = rate;
             this.shuffleBefore = shuffleBefore;
             this.killNodes = killNodes;
             //this.enemies = null;
             this.enemies = wave1;
 
             Debug.Log(enemies.Length);
             this.Startactually();
         }
         else
         {
             this.Startactually();
         }
 
 
     } }



Comment
Add comment · Show 2
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 IvovdMarel · Aug 06, 2018 at 02:07 AM 0
Share

Hi Tim,

As you are getting a null-reference in Wave.cs line 24, please provide some of the code in Wave.cs.

avatar image unity_rQfRAZYVVnSfag IvovdMarel · Aug 06, 2018 at 02:13 AM 0
Share

Uploaded the whole file. Guess then it makes a little more sense.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IvovdMarel · Aug 06, 2018 at 02:21 AM

The issue here is that your enemies array is null. Unity would serialize the array in the inspector automatically, therefore making it not-null (but just have a length of 0).

You can either initialize the enemies array in code or use the Unity inspector

Comment
Add comment · Show 1 · 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
avatar image unity_rQfRAZYVVnSfag · Aug 06, 2018 at 02:34 AM 0
Share

For the Infinity-$$anonymous$$ode i am doing it in the code and for "normal" levels I am doing it in the inspector. The Infinty $$anonymous$$ode works (as in the initialization and spawning are not infite yet) But also when the array in the inspector is not null (It is full with values) they don´t get spawned. The Debugger shows that there are values in these variables also.

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

115 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

Related Questions

How would I create a script that spawns objects more frequently as time goes on? 3 Answers

Error: Instantiated Enemies don't get hit 2 Answers

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

How do I control where enemies spawn? 1 Answer

Instantiated enemy looks at player 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