Question by
PrimeTime Gaming · Oct 01, 2015 at 01:29 AM ·
androiderrorindexout of bounds
What is wrong with my c# script? The error os ArgumentException(For Android)
If you have any advice please tell me. Thanks :)
The error I am recieving is(I am programming for Android)... ArgumentException: Index out of bounds. TouchManager.touchInput (UnityEngine.GUITexture texture) (at Assets/Scripts/TouchManager.cs:10) ButtonMovement.Update () (at Assets/Scripts/ButtonMovement.cs:28)
The Touch manager script is... It says the error is on line 10
using UnityEngine;
using System.Collections;
public class TouchManager : MonoBehaviour
{
public static bool guiTouch = false;
public void touchInput(GUITexture texture)
{
//this checks for the position of the first touch is on the button
if (texture.HitTest(Input.GetTouch(0).position))
{
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Began:
//This is where the functions happen
SendMessage("OnFirstTouchBegan", SendMessageOptions.DontRequireReceiver);
SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
guiTouch = true;
break;
case TouchPhase.Stationary:
SendMessage("OnFirstTouchStayed", SendMessageOptions.DontRequireReceiver);
SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
//This is where the functions happen
guiTouch = true;
break;
case TouchPhase.Moved:
SendMessage("OnFirstTouchMoved", SendMessageOptions.DontRequireReceiver);
SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
//This is where the functions happen
guiTouch = true;
break;
case TouchPhase.Ended:
SendMessage("OnFirstTouchEnd", SendMessageOptions.DontRequireReceiver);
//This is where the functions happen
guiTouch = false;
break;
}
}
if (texture.HitTest(Input.GetTouch(1).position))
{
switch (Input.GetTouch(1).phase)
{
case TouchPhase.Began:
//This is where the functions happen
SendMessage("OnSecondTouchBegan", SendMessageOptions.DontRequireReceiver);
SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
break;
case TouchPhase.Stationary:
SendMessage("OnSecondTouchStayed", SendMessageOptions.DontRequireReceiver);
SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
//This is where the functions happen
break;
case TouchPhase.Moved:
SendMessage("OnSecondTouchMoved", SendMessageOptions.DontRequireReceiver);
SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
//This is where the functions happen
break;
case TouchPhase.Ended:
SendMessage("OnSecondTouchEnd", SendMessageOptions.DontRequireReceiver);
//This is where the functions happen
break;
}
}
}
}
The script for the button movement is
using UnityEngine;
using System.Collections;
public class ButtonMovement : TouchManager {
public enum type {UpButton, DownButton, RightButton, LeftButton};
public type ButtonType;
public GameObject playerObject = null;
Rigidbody2D playerRigidBody = null;
public GUITexture buttonTexture = null;
//panda height control variables
public float flyHeight = 0.0f;
public float fallSpeed = 0.0f;
public float movementSpeed = 0.0f;
// Use this for initialization
void Start () {
playerRigidBody = playerObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
touchInput (buttonTexture);
}
//Began Touches. Up and Down
void OnFirstTouchBegan()
{
switch(ButtonType)
{
case type.UpButton:
playerObject.transform.Translate(Vector2.up * flyHeight);
break;
}
switch(ButtonType)
{
case type.RightButton:
playerObject.transform.Translate(Vector2.right * movementSpeed);
break;
}
}
void OnSecondTouchBegan()
{
switch(ButtonType)
{
case type.DownButton:
playerObject.transform.Translate(Vector2.down * fallSpeed);
break;
}
switch(ButtonType)
{
case type.LeftButton:
playerObject.transform.Translate(Vector2.left * movementSpeed);
break;
}
}
//TouchButtons Down and up
void OnFirstTouch ()
{
switch(ButtonType)
{
case type.UpButton:
playerObject.transform.Translate(Vector2.up * flyHeight);
break;
}
switch(ButtonType)
{
case type.DownButton:
playerObject.transform.Translate(Vector2.down * fallSpeed);
break;
}
//right button
switch(ButtonType)
{
case type.RightButton:
playerObject.transform.Translate(Vector2.right * movementSpeed);
break;
}
//left button
switch(ButtonType)
{
case type.LeftButton:
playerObject.transform.Translate(Vector2.left * movementSpeed);
break;
}
}
void OnSecondTouch ()
{
switch(ButtonType)
{
case type.UpButton:
playerObject.transform.Translate(Vector2.up * flyHeight);
break;
}
switch(ButtonType)
{
case type.DownButton:
playerObject.transform.Translate(Vector2.down * fallSpeed);
break;
}
//right button
switch(ButtonType)
{
case type.RightButton:
playerObject.transform.Translate(Vector2.right * movementSpeed);
break;
}
//left button
switch(ButtonType)
{
case type.LeftButton:
playerObject.transform.Translate(Vector2.left * movementSpeed);
break;
}
}
}
Comment
$$anonymous$$aybe you need to ensure that Input.touchCount is greater than the index for Input.GetTouch.
if (Input.touchCount > 0 && texture.HitTest(Input.GetTouch(0).position))
if (Input.touchCount > 1 && texture.HitTest(Input.GetTouch(1).position))
Answer by hexagonius · Oct 01, 2015 at 09:48 AM
Prior to accessing touch 0, you should check if there is even a touch. check touchcount > 0