Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

IndexOutOfRange

Discussion in '2D' started by samgse91, Mar 16, 2015.

?

why is throwing IndexOutOfRange exception ?

  1. gameobject is not place correctly in Unity

    0 vote(s)
    0.0%
  2. folder misplaced

    0 vote(s)
    0.0%
  1. samgse91

    samgse91

    Joined:
    Mar 16, 2015
    Posts:
    1
    Hi, we are currently having the following problem with our script, and we don't know how to solve it.
    Some help would be awesome since we believe is some very simple thing that we may be missing.

    The exception thrown when executing the following script is the 'IndexOutOfRange' exception.when it tries to
    run the following line: systems[0].transform.localEulerAngles.y < 5

    //
    // Copyright (C) 2013 Sixense Entertainment Inc.
    // All Rights Reserved
    //

    using UnityEngine;
    using System.Collections;

    public class SixenseInputTest : MonoBehaviour
    {
    public GameObject[] systems;
    public Vector3[] axes;
    private Vector3[] a;
    public float[] speed = new float[] { 30, -15, 15, -35, 35, 200 };
    public float robotBaseSliderValue = 0.0F;
    public float robotColumnSliderValue = 0.0F;
    public float robotLinkArmValue = 0.0F;
    public float robotInWristValue = 0.0F;
    public float robotWristValue = 0.0F;
    public float robotOutWristValue = 0.0F;
    private int flag1 = 0;
    private int flag2 = 0;
    private int flag3 = 0;
    private int flag5 = 0;
    uint buttonSelected = 0;
    uint controlSelected = 0;
    public int ax;
    void Start()
    {
    axes = new Vector3[6];

    axes[0] = new Vector3(0, 1, 0);
    axes[1] = new Vector3(0, 0, 1);
    axes[2] = new Vector3(0, 0, 1);
    axes[3] = new Vector3(1, 0, 0);
    axes[4] = new Vector3(0, 0, 1);
    axes[5] = new Vector3(1, 0, 0);

    a = new Vector3[6];

    a[0] = Vector3.zero;
    a[1] = Vector3.zero;
    a[2] = Vector3.zero;
    a[3] = Vector3.zero;
    a[4] = Vector3.zero;
    a[5] = Vector3.zero;

    ax = systems.Length;
    }



    void move(uint pzaSel, double valJoyStick, uint controlSel)
    {
    Debug.Log("Has seleccionado la pieza = " + pzaSel + " el valor del joystick es: " + valJoyStick);

    //Logica para el control derecho
    if ((pzaSel == 1) && (controlSel == 2))
    {
    pzaSel = 5;
    }
    else if ((pzaSel == 2) && (controlSel == 2))
    {
    pzaSel = 6;
    }
    else if ((pzaSel == 3) && (controlSel == 2))
    {
    pzaSel = 7;
    }
    else if ((pzaSel == 4) && (controlSel == 2))
    {
    pzaSel = 8;
    }

    switch (pzaSel)
    {
    //Control Izquierdo
    case 1:
    Debug.Log("Dentro del switch Has seleccionado la pieza = " + pzaSel + " el valor del joystick es: " + valJoyStick);
    valJoyStick = 0.7;
    if (valJoyStick > 0)
    {
    if (systems[0].transform.localEulerAngles.y < 5)
    {
    flag1 = 0;
    }
    a[0] += axes[0] * speed[0] * Time.deltaTime;
    systems[0].transform.localEulerAngles = a[0];
    if (systems[0].transform.localEulerAngles.y > 170 && flag1 == 0)
    {
    systems[0].transform.localEulerAngles = new Vector3(0, 170, 0);
    a[0] -= axes[0] * speed[0] * Time.deltaTime;
    }
    }
    else if (valJoyStick < 0)
    {
    if (systems[0].transform.localEulerAngles.y > 355)
    {
    flag1 = 1;
    }
    a[0] += axes[0] * -1 * speed[0] * Time.deltaTime;
    systems[0].transform.localEulerAngles = a[0];
    if (systems[0].transform.localEulerAngles.y < 210 && flag1 == 1)
    {
    systems[0].transform.localEulerAngles = new Vector3(0, 210, 0);
    a[0] -= axes[0] * -1 * speed[0] * Time.deltaTime;
    }
    }
    break;
    case 2:

    break;
    case 3:

    break;
    case 4:

    break;
    //Control derecho
    case 5:
    Debug.Log("Dentro del switch Has seleccionado la pieza = " + pzaSel + " el valor del joystick es: " + valJoyStick);
    break;
    case 6:

    break;
    case 7:

    break;
    case 8:

    break;
    default:
    if (valJoyStick == 0.0)
    {
    buttonSelected = 0;
    robotBaseSliderValue = 0;
    robotColumnSliderValue = 0;
    robotLinkArmValue = 0;
    robotInWristValue = 0;
    robotWristValue = 0;
    robotOutWristValue = 0;
    }
    break;
    }

    }

    void Update()
    {
    for (uint i = 0; i < 2; i++)
    {
    if (SixenseInput.Controllers != null)
    {
    uint controllerNumber = i + 1;

    uint buttonsCount = 0;
    //uint buttonSelected = 0;
    //uint controlSelected = 0;
    string buttonsText = "";
    string buttonsText2 = "";


    if (SixenseInput.Controllers.Enabled)
    {
    foreach (SixenseButtons button in System.Enum.GetValues(typeof(SixenseButtons)))
    {
    if (SixenseInput.Controllers.GetButton(button) && (buttonsCount < 4))
    {
    if (buttonsText != "")
    {
    buttonsText += " | ";
    }
    buttonsText += button;
    buttonsCount++;
    }
    else if (SixenseInput.Controllers.GetButton(button) && (buttonsCount >= 4))
    {
    if (buttonsText2 != "")
    {
    buttonsText2 += " | ";
    }
    buttonsText2 += button;
    buttonsCount++;
    }

    if (SixenseInput.Controllers.GetButtonDown(button))
    {
    Debug.Log("Pressed = " + button + " el control es: " + controllerNumber);


    if (buttonsText.Equals("ONE"))
    {
    buttonSelected = 1;
    controlSelected = controllerNumber;
    Debug.Log("Seleccion = " + buttonSelected + " el control es: " + controllerNumber);
    }

    if (buttonsText.Equals("TWO"))
    {
    buttonSelected = 2;
    Debug.Log("Seleccion = " + buttonSelected + " el control es: " + controllerNumber);
    }

    if (buttonsText.Equals("THREE"))
    {
    buttonSelected = 3;
    Debug.Log("Seleccion = " + buttonSelected + " el control es: " + controllerNumber);
    }

    if (buttonsText.Equals("FOUR"))
    {
    buttonSelected = 4;
    Debug.Log("Seleccion = " + buttonSelected + " el control es: " + controllerNumber);
    }


    }

    //if ( SixenseInput.Controllers.GetButtonUp( button ) )
    //{
    // Debug.Log( "Released = " + button );
    //}
    }
    }

    }


    if (SixenseInput.Controllers.Enabled)
    {
    //guiText.guiText.text += SixenseInput.Controllers.JoystickX;
    Debug.Log("Joystick X value = " + SixenseInput.Controllers.JoystickX + " el control es: ");
    double val = SixenseInput.Controllers.JoystickX;
    move(buttonSelected, val, controlSelected);
    }


    if (SixenseInput.Controllers.Enabled)
    {
    //guiText.guiText.text += SixenseInput.Controllers.JoystickY;
    Debug.Log("Joystick Y value = " + SixenseInput.Controllers.JoystickY + " el control es: ");
    }



    }
    }
    }
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    My best guess is that the systems array, which is destined to be an array of GameObjects, has not been initialized. You can't read the length until it has been created. I think you need a start or awake function, which says like 'if ( systems == null){systems = new GameObect[20];}' or whatever... to set it up to exist. Then you can assign objects to it in the inspector or whatever.
     
  3. BlueWoozle

    BlueWoozle

    Joined:
    Mar 16, 2015
    Posts:
    8
    Index out of range would suggest that there's nothing in the systems array, and you're trying to access an element that's empty.
    Does the array that you're passing in through the inspector have any elements in it? Try logging the value of ax in your Start() function to see if the array is empty.