Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Stevorino · Aug 30, 2013 at 01:38 AM · c#arrayprogrammingindex

Index Out of Range Exception - can't figure it out

The error is being thrown up in Start() on Line 2

timeRemaining = timeUntilNextAttack[k];

I can't figure this one out.... the index is 10 floats long, assigned in the inspector.

using System.Collections;

 public class GameManager : MonoBehaviour {
         
     
 //Timer Variables
 public float[] timeUntilNextAttack;
 private float timeRemaining;
 public UILabel timerCountdown;
 private int days, hours, minutes, seconds, currentAttack;
 private string daytext, hourtext, minutetext, secondtext;
 public GameObject airEnemies, groundEnemies;
     
 //SpawnPoints
 public Transform[] northGroundSpawns, eastGroundSpawns, westGroundSpawns, southGroundSpawns;
 public Transform[] northAirSpawns, eastAirSpawns, westAirSpawns, southAirSpawns;
 public Transform[] caveSpawns;
     
 //Attack Wave Specific Variables    
 public int[] attack1WaveNumber;
 public int[] attack1EnemyNumber;
 public GameObject[] attack1EnemyType;
 public string[] attack1EnemyDirection;
 public int[] attack1EnemyLevel;
 
                                             
     //begins initial timer at start of game. 
     //Subsequent timers set at end of attack 

public void Start(){

         int k = 0;
         timeRemaining = timeUntilNextAttack[k];
         
         SetAttackTimer();
         
     }
     
     //starts timer
     public void SetAttackTimer(){
         
         
     
         InvokeRepeating ("Countdown", 1.0f, 1.0f);    
     
     }    
     
     //actual timer and relay of information to in-game text displaying timer
     public void Countdown(){
         Debug.Log (timeRemaining+" second remaining");
         timeRemaining -= 1f;
         days    = Mathf.FloorToInt(timeRemaining / 86400F); 
         hours   = Mathf.FloorToInt((timeRemaining - (days*86400)) / 3600F); 
         minutes = Mathf.FloorToInt((timeRemaining - (days*86400)-(hours*3600)) / 60F);
            seconds = Mathf.FloorToInt(timeRemaining - (days*86400)-(hours*3600)-(minutes*60));
          
         if(days > 1)daytext = " days, ";
         else if(days == 1)daytext = " day, ";
         else if(days < 1){
             days = 0;
             daytext = "";
             }
         if(hours > 1)hourtext = " hours, ";
         else if(hours == 1)hourtext = " hour, ";
         else if(hours < 1){
             if(days < 1){
                 hours = 0;
                 hourtext = " ";
                 }
             else{
                 hourtext = " hours, ";
                 }
             }
         if(minutes > 1)minutetext = " mins, ";
         else if(minutes == 1)minutetext = " min, ";
         else if(minutes < 1){
             if(hours < 1 || days < 1){
                 minutes = 0;
                 minutetext = " ";
                 }
             else{
                 minutetext = " mins, ";
                 }
             }
         if(seconds > 1)secondtext = " secs";
         else if(seconds == 1)secondtext = " sec";
         else if(seconds < 1){
             if(hours < 1 || days < 1 || minutes < 1){
                 seconds = 0;
                 secondtext = " ";
                 }
             else{
                 secondtext = " secs";
                 }
             }
         timerCountdown.text = days+daytext+hours+hourtext+minutes+minutetext+seconds+secondtext;
         
         //timer is up, start the attack and cancel the timer
         if(timeRemaining <=0){
             StartAttack();
             CancelInvoke("Countdown");
              }
     }
 
     //initiates the oncoming attack
     public void StartAttack(){
     Debug.Log ("Start Attack!");    
     if(currentAttack == 0)Attack1();    
     }
     
     //Attack # 1
     public void Attack1(){
             
         
     //find max wave number and replace timer with wave of wave count
     int maxWave = 0;
     for (int i = 0; i < attack1WaveNumber.Length; i++)
         {
             if (attack1WaveNumber[i] > maxWave)
         {
         maxWave = attack1WaveNumber[i];    
             }}
     timerCountdown.text = "Wave 1 of "+maxWave;
     Debug.Log (maxWave+" Is the max wave");
     
     //for each wave # in Attack#Wave Number...instantiate guys from other arrays    
     for (int j = 0; j < attack1WaveNumber.Length; j++)
         {
             
                 if (attack1WaveNumber[j] == 1)
                     {
                                                     
                         if(attack1EnemyDirection[j] == "north"){
                     
                                 if(attack1EnemyType[j].tag == "groundenemy")
                                     {    int spawnChoice = Random.Range(0,northGroundSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], northGroundSpawns[spawnChoice].position, northGroundSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = groundEnemies.transform;
                                                 }
                                     }
                                 else if(attack1EnemyType[j].tag == "airenemy")
                                     {    int spawnChoice = Random.Range(0,northAirSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], northAirSpawns[spawnChoice].position, northAirSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = airEnemies.transform;
                                             }
                                     }
                             }
                     else if(attack1EnemyDirection[j] == "east"){
                     
                                 if(attack1EnemyType[j].tag == "groundenemy")
                                     {    int spawnChoice = Random.Range(0,eastGroundSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], eastGroundSpawns[spawnChoice].position, eastGroundSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = groundEnemies.transform;
                                                 }
                                     }
                                 else if(attack1EnemyType[j].tag == "airenemy")
                                     {    int spawnChoice = Random.Range(0,eastAirSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], eastAirSpawns[spawnChoice].position, eastAirSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = airEnemies.transform;
                                             }
                                     }
                             }
                     if(attack1EnemyDirection[j] == "west"){
                     
                                 if(attack1EnemyType[j].tag == "groundenemy")
                                     {    int spawnChoice = Random.Range(0,westGroundSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], westGroundSpawns[spawnChoice].position, westGroundSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = groundEnemies.transform;
                                                 }
                                     }
                                 else if(attack1EnemyType[j].tag == "airenemy")
                                     {    int spawnChoice = Random.Range(0,westAirSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], westAirSpawns[spawnChoice].position, westAirSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = airEnemies.transform;
                                             }
                                     }
                             }
                     if(attack1EnemyDirection[j] == "south"){
                     
                                 if(attack1EnemyType[j].tag == "groundenemy")
                                     {    int spawnChoice = Random.Range(0,southGroundSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], southGroundSpawns[spawnChoice].position, southGroundSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = groundEnemies.transform;
                                                 }
                                     }
                                 else if(attack1EnemyType[j].tag == "airenemy")
                                     {    int spawnChoice = Random.Range(0,southAirSpawns.Length);
                                     
                                             //spawn number of enemy above at place above
                                             int numberToSpawn = attack1EnemyNumber[j];
                                             for (int k = 0; k < numberToSpawn; k++) {
                                                 GameObject newEnemy = Instantiate(attack1EnemyType[j], southAirSpawns[spawnChoice].position, southAirSpawns[spawnChoice].rotation) as GameObject;
                                                 Debug.Log (newEnemy.name);
                                                 newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
                                                 newEnemy.transform.parent = airEnemies.transform;
                                             }
                                     }
                             }
                 
                 
                     }
             }
                     
     }
     
     public void AttackWin(){
     
         
         //setup next timer
         currentAttack += 1;
         int i = currentAttack;
         //timeRemaining = timeUntilNextAttack[i];
         SetAttackTimer();
         
         
     }
     
     
 
 }
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
2
Best Answer

Answer by clunk47 · Aug 30, 2013 at 01:43 AM

Are you filling in the values of timeUntilNextattack in the inspector? You could fill your array in Start() like so for testing:

 timeUntilNextAttack = new float[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
Comment
Add comment · Show 9 · 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 Stevorino · Aug 30, 2013 at 01:51 AM 1
Share

First off, thanks so much for the fast response - wasn't expecting anything until tomorrow!

When I put this line of code directly above the problem in Start(), it eli$$anonymous$$ates that issue and then creates a new error at line 65.

Not sure what that means about the original issue?

avatar image clunk47 · Aug 30, 2013 at 01:56 AM 1
Share

Please give the error on line 65.

avatar image Stevorino · Aug 30, 2013 at 02:04 AM 1
Share

NullReferenceException: Object reference not set to an instance of an object

Game$$anonymous$$anager.Countdown()(at Assets/Resources/Game$$anonymous$$anager.cs:173)

avatar image clunk47 · Aug 30, 2013 at 02:28 AM 1
Share

You're adding ints to a string without converting ToString(). That may be causing an issue, but as far as the null issue goes, make sure NOTHING is null, EVERYTHING has been assigned a value. days, dayText, hours, hourText, and so on.

 timerCountdown.text = 
 days.ToString()
 +daytext
 +hours.ToString()
 +hourtext
 +$$anonymous$$utes.ToString()
 +$$anonymous$$utetext
 +seconds.ToString()
 +secondtext;
avatar image ArkaneX · Aug 30, 2013 at 08:56 AM 2
Share

Have you initialized timerCountdown or is it unassigned? In my opinion this is the case of exception.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Array index help. 1 Answer

Convert To Using Array 2 Answers

Help using LitJson 1 Answer

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 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