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 FerrenReve · Aug 27, 2015 at 09:02 PM · arraytrigger

IndexOutOfRangeException error

Can't find nor solve what is causing the error.

IndexOutOfRangeException: Array index is out of range.Arrays.OnTriggerStay(UnityEngine.Collider other) (at Assets/Scripts/Arrays.cs:89)

This is the script using UnityEngine; using System.Collections;

 public class Arrays : MonoBehaviour
 {
     public int numSelectors;
     public int xPosition; //must be rounded to the nearest 10
     public int yPosition;
     public int zRotation;
     public GameObject[] destruct;
     public GameObject[] selectorArr;
     public GameObject selector; //selected in the editor
     public GameObject wirePrefab;
     public bool verticalDirection;
     public bool leftKeyPressed;
     public bool rightKeyPressed;
     public string deactivationName;
     public bool wireSpawned;
     public int total;
 
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.LeftArrow)) {
             //Debug.Log ("left key was pressed");
             leftKeyPressed = true;
             //Debug.Log ("true");
         }
         if (Input.GetKeyUp (KeyCode.LeftArrow)) {
             //Debug.Log ("left key was released");
             leftKeyPressed = false;
             //Debug.Log ("false");
         }
         
         if (Input.GetKeyDown (KeyCode.RightArrow)) {
             //Debug.Log ("right key was pressed");
             rightKeyPressed = true;
             //Debug.Log ("true");
         }
         if (Input.GetKeyUp (KeyCode.RightArrow)) {
             //Debug.Log ("right key was released");
             rightKeyPressed = false;
             //Debug.Log ("false");
         }
     }
 
 
     void Start()
     {
 
     }
 
             void OnTriggerStay(Collider other){
 
         selectorArr = new GameObject[numSelectors];
         for (int i = 0; i < numSelectors; i++) {
             GameObject[]destruct = GameObject.FindGameObjectsWithTag (deactivationName);
             int destructCount = destruct.Length;
 
             if (other.gameObject.tag == "Player") {
                 selector.gameObject.tag = deactivationName;
             if (rightKeyPressed == true) {
                     if (wireSpawned == false) {
 
                     //if (GameObject.Find ("Pfb_PPlateOFF").GetComponent<PresurePlate> ().doorSpawned) { //will check if true
             if (verticalDirection == false) {
             GameObject go = Instantiate(selector, new Vector3((i * 16f) + xPosition, yPosition, -6), Quaternion.Euler(0,0,zRotation)) as GameObject;
             go.transform.localScale = Vector3.one;
             selectorArr[i] = go;
             }
             if (verticalDirection == true) {
                 GameObject go = Instantiate(selector, new Vector3(xPosition, (i * 16f) + yPosition, 0), Quaternion.Euler(0,0,zRotation)) as GameObject;
                 go.transform.localScale = Vector3.one;
                 selectorArr[i] = go;
 
                 }
                         if (destructCount > total) {
                         wireSpawned = true;
                         //Debug.Log("successful spawn");
                         }
             }
                 }
             }
 
             if (leftKeyPressed == true) {
                 if (other.gameObject.tag == "Player") {
                         //if (wireSpawned == true){
 
 
                     Destroy (destruct [i]);
                             wireSpawned = false;
                         //foreach (GameObject goHolder in destruct) Destroy(goHolder);
                         //Destroy(go);
                             //Debug.Log("successful despawn");
 
                         //}
                     }
                 }
 
         }
     }
 
     void OnTriggerExit(Collider other){
         selector.gameObject.tag = ("wire");
     }
 
 }

Assistance and advices would be appreciated.

Thanks

Comment
Add comment · Show 1
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 Owen-Reynolds · Aug 28, 2015 at 12:34 AM 0
Share

$$anonymous$$oved to the new Help Room, which is the appropriate place for Debugging Qs.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Aug 27, 2015 at 09:16 PM

On the line with Destroy (destruct [i]); you're trying to access the array with an index greater than the length of the array so it's throwing the error. To solve it try checking if i < destruct.Length before trying to access it.

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 FerrenReve · Aug 28, 2015 at 12:51 AM 0
Share

Yes it helped, thank you very much.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

problems with trigger,some doubts about my script / algumas duvidas sobre o meu script 0 Answers

Trigger detects player collision and play specific audio from array 1 Answer

How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer

Enable Multiple Box Collides From Parent Object 2 Answers

How do you rotate objects at the same time using arrays and triggers 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