• 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 ActiveMars91 · Jan 14, 2021 at 04:51 PM · arrays

I'm being defeated by IndexOutOfRangeException.

Hey all. So I've taken a somewhat roundabout way to spawning enemies, randomly selected based on difficulty. I keep getting this error message pop up:

 IndexOutOfRangeException: Index was outside the bounds of the array.
 (wrapper stelemref) System.Object.virt_stelemref_class_small_idepth(intptr,object)
 ItemDatabase.SpawnSelect (System.Int32 easyEnemyCount, System.Int32 mediumEnemyCount, System.Int32 toughEnemycount) (at Assets/Scripts/ItemDatabase.cs:127)
 
 WaveSpawner+<SpawnWave>d__14.MoveNext () (at Assets/Scripts/WaveSpawner.cs:163)
 UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <49f4e7e791cc4fffacd88f729e2b1e4c>:0)



I'm fairly new to C# and coding in general. I'm really having some trouble figuring out where I'm going wrong. any help understanding this would be amazing. Thanks in advance.

  public void SpawnSelect(int easyEnemyCount, int mediumEnemyCount, int toughEnemycount)
     {
 
         Items[] spawnerEasy = new Items[easyEnemyCount];
         Items[] spawnerMedium = new Items[mediumEnemyCount];
         Items[] spawnerTough = new Items[toughEnemycount];
 
         Array[] spawner1 = new Array[3];
         Array[] spawner2 = new Array[3];
         Array[] spawner3 = new Array[3];
         Array[] spawner4 = new Array[3];
         Array[] spawner5 = new Array[3];
 
         for (int location = 0; location < 4; location++)
         {
             if (spawner1Full == false)
             {
                 for (int i = 0; i < easyEnemyCount; i++)
                 {
 
 
                     int easyEnemyType = UnityEngine.Random.Range(0, easyCreatures.Length);
                     Items easyEnemy = easyCreatures[easyEnemyType];
                     Debug.Log("The easy opponent is a " + easyEnemy.name);
                     Debug.Log("spawnerEasy is " + spawnerEasy);
                     spawnerEasy[i - 1] = easyEnemy;  //Error Here!
                     
                     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                     // Above is the line provided by some of the error messages. 
                     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
                 }
 
                 for (int i = 0 + easyEnemyCount; i < mediumEnemyCount + easyEnemyCount; i++)
                 {
                     int mediumEnemyType = UnityEngine.Random.Range(0, mediumCreatures.Length);
                     Items mediumEnemy = mediumCreatures[mediumEnemyType];
                     spawnerMedium[i - 1] = mediumEnemy;
                     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                     // Above is the line provided by a small number of other error messages. 
                     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                 }
 
                 for (int i = 0 + easyEnemyCount + mediumEnemyCount; i < toughEnemycount + easyEnemyCount + mediumEnemyCount; i++)
                 {
                     int toughEnemyType = UnityEngine.Random.Range(0, toughCreatures.Length);
                     Items toughEnemy = toughCreatures[toughEnemyType];
                     spawnerTough[i - 1] = toughEnemy;
                 }
                 spawner1[0] = spawnerEasy;
                 spawner1[1] = spawnerMedium;
                 spawner1[2] = spawnerTough;
                 spawner1Full = true;
                 Debug.Log("Spawner 1 is full... moving onto spawner 2.");
             }


The values being passed into SpawnSelect() are randomly generated on the script that calls the method. using this code:



  IEnumerator SpawnWave (Wave _wave)
     {
         int easyEnemies = Random.Range(0, 5);
         int mediumEnemies = Random.Range(0, 3);
         int toughEnemies = Random.Range(0, 0);
         // Set the "state" to "SPAWNING",...
         state = SpawnState.SPAWNING;
         // add one to the value of the for loop unit the "Wave.count" value is reached.
         for (int i = 0; i < _wave.count; i++)
         {
             // Each time the for loop itterates, run the SpawnEnemy function,
             SpawnEnemy(_wave.enemy);
             // but wait for this long before starting the next itteration of the for loop.
             yield return new WaitForSeconds(1f / _wave.rate);
         }
         
         // Change the current "state" from "SPAWNING" to "WAITING"...
         state = SpawnState.WAITING;
         itemDatabase.SpawnSelect
             (easyEnemies, 
             mediumEnemies, 
             toughEnemies);
         // before leaving the iEnumerator.
         yield break;
     }



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
0
Best Answer

Answer by CmdrZin · Jan 14, 2021 at 05:50 PM

 spawnerEasy[i - 1] = easyEnemy;  //Error Here!

i = 0 the first time through. So i-1 = -1 which is not a valid index for an array.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

112 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

Related Questions

Creating an inventory/weapon array and ability to scroll through it 1 Answer

text fields and GUI 1 Answer

Creating two arrays in one script and using them outside that script 1 Answer

JavaScript Grid Array Size 0 Answers

How do I use array's to spawn gameobjects(C#)? 3 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