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 crjowen1993 · Jan 27, 2019 at 01:52 PM · indexoutofrangeexception

IndexOutOfRangeException - beginner. Please help!

Hello everyone,

I am trying to get this script to work, but am having some issues. I have some enemies spawn (I have limited it to one whilst I try to get the to work to check the Debug.Log values) and as soon as they are in range, I call the function Turn() below. The Script works fine until this line (57):

charlist[i]=enemies[i];

Error as below:

IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr,object) BattleStats.Turn () (at Assets/BattleStats.cs:57) CameraMovement.Update () (at Assets/CameraMovement.cs:70)

and all of the Debug. Logs before this appear to be correct. And enemies.Length > 1, so enemies[0] should be fine. I am guessing something is wrong with me writing charlist[0], but I don't know why. enemies[] is the list of all enemies and charlist[] is a list of all characters (including all enemies).

If someone could explain that would be really helpful. Thanks.


using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class BattleStats : MonoBehaviour {

 public GameObject[] enemies;
 public GameObject[] player;
 public int Ambusha=0;
 public int Ambushb=0;
 public bool Ambush=false;
 public bool startfight = true;
 public GameObject[] charturn;
 public GameObject[] charlist;
 public GameObject Temphighest;
 public int charactersNumber;
 public Canvas battleUI;
 public GameObject[] TurnText;

 // Start is called before the first frame update
 void Start()
 {
 }

 // Update is called once per frame
 void Update()
 {
 }



 public void Turn()
 {
     enemies= GameObject.FindGameObjectsWithTag ("Enemy");
     player = GameObject.FindGameObjectsWithTag ("Player");

     if (startfight == true){
     for (int i=0; i < enemies.Length; i++) 
     {
             Debug.Log("Hello");
             Debug.Log(enemies.Length);
             Debug.Log(enemies[i].GetComponent<Attributes> ().charname);
         Ambusha += enemies[i].GetComponent<Attributes>().luck + enemies[i].GetComponent<Attributes>().speed;
             Debug.Log(Ambusha);
         charlist[i]=enemies[i]; 
             Debug.Log(charlist[i].GetComponent<Attributes> ().charname);
     }

     for (int j=0; j < player.Length; j++) 
     {
             Debug.Log("Hello2");
         Ambushb += player[j].GetComponent<Attributes>().luck + player[j].GetComponent<Attributes>().speed;
         charlist[enemies.Length+j+1]=player[j]; 
             Debug.Log(charlist[enemies.Length+j+1].GetComponent<Attributes> ().charname);
     }

     if (Ambusha > Ambushb)
         {Ambush = true;} 


         startfight = false;

         }
         
     charactersNumber= enemies.Length+player.Length;

     for (int j=0; j<5 ;j++){



         
     for (int i=0; i < charactersNumber ; i++)
     {
         if (i==0){
             Temphighest = charlist[0];}
         if (charlist[i].GetComponent<Attributes>().currentspeed > Temphighest.GetComponent<Attributes>().currentspeed)
             {Temphighest = charlist[i];}
             
     }

         if (Temphighest.GetComponent<Attributes>().currentspeed<50){
             for (int k=0; k < charactersNumber;k++){
                 if (charlist[k].GetComponent<Attributes>().affdead == 0)
                 {
                     charlist[k].GetComponent<Attributes>().currentspeed=charlist[k].GetComponent<Attributes>().speed;
                     j = j-1;
                 }
             } 
         }

         else {


             if (Ambush == false){
         charturn[j]=Temphighest;
                 Temphighest.GetComponent<Attributes>().currentspeed -= 50;}


         else if(Ambush== true){
                 for(int k=0; k<enemies.Length;k++){
                     charturn[j+k]=enemies[k];
                     Ambush = false;
                     j=enemies.Length-1;
             

                 }




             }
         }

         
             
     }

 
     for (int i=1;i<5;i++)
     {
     TurnText[0] = GameObject.FindGameObjectWithTag ("Turn"+i);
     TurnText[0].GetComponent<Text>().text= charturn[0].GetComponent<Attributes>().charname;
     }



 }

 void Battle()
 {
 }






}

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

Answer by tormentoarmagedoom · Jan 27, 2019 at 04:57 PM

Good day.

This kind of errors must be solved by you. As error says, some array index calue is out of range. For example, if you try to acess the enemies[5] but enemies array only have 3 elements. The index is out of range of the array.

Bye.

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 crjowen1993 · Jan 28, 2019 at 06:53 PM 0
Share

I managed to solve the error - I hadn't actually specified the size of the array in the Unity inspector, hence it thought the size was 0.

I really appreciate your response though. I am actually having difficulty with my animation, if you are knowledgeable with that?

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

164 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 avatar image avatar image avatar image avatar image 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

JSON loading causes WebGL build index out of range exceptions 2 Answers

Array index is out of range (C#)? 1 Answer

problem with index out of bounds 0 Answers

I want to make make a plant to grow after few seconds. 0 Answers

How to GetComponent for SteamVR_TrackedObj when script not attached to controller 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