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. Dismiss Notice

UnityException: Index out of bounds. Tried everything, any ideas why?

Discussion in 'Scripting' started by Rutenis, Nov 29, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Hey, so how should i fix this error? "UnityException: Index out of bounds" ive tried using:
    Code (csharp):
    1. if(Input.touchCount > 0)
    2. {
    3. //My code
    4. }
    But it doesnt seem to work. Any ideas? The specific line that i get an error on is this one:
    Code (csharp):
    1.  
    2.             float x = Input.GetTouch (touch2Watch).position.x / Screen.width,
    3.  
    -Thanks!

    Full code:
    Code (csharp):
    1.  
    2. Vector3 MoveJoyStick()
    3.     {
    4.         if(Input.touchCount > 0)
    5.         {
    6.             float x = Input.GetTouch (touch2Watch).position.x / Screen.width,
    7.             y = Input.GetTouch (touch2Watch).position.y / Screen.height;
    8.             position = Vector3.ClampMagnitude(new Vector3 (x-oJoyPos.x, y-oJoyPos.y, 0), maxJoyDelta) + oJoyPos;
    9.    
    10.             joyDelta = new Vector3(position.x - oJoyPos.x, position.y - oJoyPos.y, 0);
    11.         }
    12.             return position;
    13.     }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Whats the value of touch2Watch
    Presumably its greater than the number of touches
     
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Its the maximum amount of touches.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The number in GetTouch can't be equal to or greater than the current number of touches. If touchCount > 0, then it could be as low as 1, which would mean only GetTouch(0) would work, and anything higher is out of bounds. You must always check for the current number of touches before using GetTouch.

    --Eric
     
  5. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Yeah, fixed it.

    Code (csharp):
    1.  
    2. if(Input.touchCount > touch2Watch || Input.touchCount > touch2Watch)
    3.         {
    4.             float x = Input.GetTouch (touch2Watch).position.x / Screen.width,
    5.             y = Input.GetTouch (touch2Watch).position.y / Screen.height;
    6.            
    7.             Vposition = Vector3.ClampMagnitude(new Vector3 (x-oJoyPos.x, y-oJoyPos.y, 0), maxJoyDelta) + oJoyPos;
    8.            
    9.            
    10.             joyDelta = new Vector3(Vposition.x - oJoyPos.x, Vposition.y - oJoyPos.y, 0);
    11.         }
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  7. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Yeah, i guess so because you cant see the rest of the script. Im not gonna bother copying that and showing to you, but thanks for the help guys! :)