Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Garndaddy · Feb 28, 2014 at 12:56 AM · javascriptarraytower-defensearray-out-of-range-except

Array index is out of range

I've been following the Tower Defense tutorials on cgcookie.com and am in the process of creating the waves for the game. I'm getting the "array index is out of range error" when I start the game. The error occurs in the SpawnNewEnemy function at the bottom of the script. I have read a lot of similar questions on Unity Answers but none of them have solved the problem for me. I went ahead and posted the entire script just in case I messed up somewhere else. Any ideas on what I'm doing wrong?

pragma strict

 static var playerDamage = 0;
 
 var waveActive : boolean = false;
 var spawnEnemies : boolean = false;
 
 var healthCount : int = 100;
 var scoreCount : int = 0;
 var moneyCount : int = 0;
 
 var waveText : UILabel;
 var scoreText : UILabel;
 var moneyText : UILabel;
 var healthText : UILabel;
 
 var waveLevel : int = 0;
 var difficultyMultiplier : float = 1.0;
 var waveLength : float = 20.0;
 var intermissionTime : float = 45.0;
 private var waveEndTime : float = 0;
 
 var enemyPrefabs : GameObject[];
 var shooterSpawns : Transform;
 private var shooterSpawnPoints : Transform[];
 var respawnMinBase : float = 3.0;
 var respawnMaxBase : float = 10.0;
 private var respawnMin : float = 3.0;
 private var respawnMax : float = 10.0;
 var respawnInterval : float = 2.5;
 var enemyCount : int = 0;
 private var lastSpawnTime : float = 0;
 
 
 function Start () {
 
     shooterSpawnPoints = new Transform[shooterSpawns.childCount];
     var i : int = 0;
     for( var theSpawnPoint : Transform in shooterSpawns) {
         shooterSpawnPoints[i] = theSpawnPoint;
         i++;
     }
 
     SetNextWave();
     StartNewWave();
 
 }
 
 function Update () {
 
     if(waveActive) {
         if(Time.time >= waveEndTime) {
             spawnEnemies = false;
             if(enemyCount ==0) {
                 FinishWave();
             }
         }
         
         if(spawnEnemies) {
             if(Time.time > (lastSpawnTime + respawnInterval)) {
                 SpawnNewEnemy();
             }
         }
         
     }
 
 }
 
 function UpdateHUD() {
     waveText.text = "Wave: " + waveLevel;
     scoreText.text = "Score: " + scoreCount;
     healthText.text = "Health: " + healthCount;
     moneyText.text = "Money: " + moneyCount;
 }
 
 function FinishWave() {
     waveActive = false;
 
     yield WaitForSeconds(intermissionTime);
     
     SetNextWave();
     StartNewWave();
     
 }
 
 function SetNextWave() {
     waveLevel++;
     difficultyMultiplier = ((Mathf.Pow(waveLevel,2)) * .005) + 1;
     respawnMin = respawnMinBase * (1/difficultyMultiplier);
     respawnMax = respawnMaxBase * (1/difficultyMultiplier);
 
 }
 
 function StartNewWave() {
     
     UpdateHUD();
     SpawnNewEnemy();
     
     waveEndTime = Time.time + waveLength;
     
     waveActive = true;
     spawnEnemies = true;
     
 }
 
 function SpawnNewEnemy() {
 
     var enemyChoice = Random.Range(0,enemyPrefabs.length);
     
     var spawnChoice : int;
     if(enemyPrefabs[enemyChoice].tag == "Shooter Enemy") {
         spawnChoice = Random.Range(0, shooterSpawnPoints.length);
         Instantiate(enemyPrefabs[enemyChoice], shooterSpawnPoints[spawnChoice].position, shooterSpawnPoints[spawnChoice].rotation);
     }
 
     else {
         //add other enemy types here
     }
 
     enemyCount++;
     
     lastSpawnTime = Time.time;
     
     respawnInterval = Random.Range(respawnMin, respawnMax);
 
 }
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 robertbu · Feb 28, 2014 at 12:59 AM

It is always helpful to folks attempting to answer your question if you can give direct them to the line that is giving the error. Usually you can do that by copying and pasting the error message from the console into the question.

I'm guessing the error is on line 111. This will happen if the 'enemyPrefabs' array has a length of 0. It appears that this array is to be initialized by drag and drop in the inspector. If you find that the array is initialized, check to make sure this script is not attached to more than one game object.

Comment
Add comment · Show 6 · 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 Garndaddy · Feb 28, 2014 at 01:09 AM 0
Share

Here is the error message:

IndexOutOfRangeException: Array index is out of range. Level$$anonymous$$aster.SpawnNewEnemy () (at Assets/$$anonymous$$y Scripts/Level$$anonymous$$aster.js:113)

I checked all the gameobjects in my scene and the ones that will be instantiated and none of them have the script attached. It says line 113 in the error message which is this line:

Instantiate(enemyPrefabs[enemyChoice], shooterSpawnPoints[spawnChoice].position, shooterSpawnPoints[spawnChoice].rotation);

avatar image robertbu · Feb 28, 2014 at 02:36 AM 0
Share

So I was right about the line of the error. The issue is the initialization of the enemyPrefab array. Did you check the array in the Inspector to make sure it is initialized with enemy prefabs?

avatar image Garndaddy · Feb 28, 2014 at 02:41 AM 0
Share

I did. I also changed the amount of prefabs I had in the array just to make sure it wasn't because of the random range or something. I might just copy the whole script to a new one and reattach it to my game object.

avatar image Garndaddy · Feb 28, 2014 at 03:31 AM 0
Share

I tried putting a different prefab in the array with a different tag and the error did not appear. When I changed the tag to the one in the script is when the error appears. Don't know if this will help but I thought it was weird because nothing instantiates but the error disappears.

avatar image robertbu · Feb 28, 2014 at 04:40 AM 0
Share

Looking deeper, there is another potential for your error. 'shooterSpawns' needs to be initialized. In particular, it appears that 'shooterSpawns' should be a parent object with a number of child objects at the positions where you want to span objects. Is this how you have your game setup? Did you initialize 'shooterSpawns' and if so, does it have children that are at the spawn point locations?

Show more comments

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

20 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

Related Questions

Tower defense, turret aiming wrong. 1 Answer

Loop Through Entire Array - Simon Game 1 Answer

Obtaining a Array with Unique Items 1 Answer

3D array in Unity script (javascript) + debuging 1 Answer

Edit Prefab, Instantiate, Access Array in Clone 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