• 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 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
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 Yword · Oct 01, 2015 at 03:07 AM 0
Share

$$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))

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · 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
0

Answer by PrimeTime Gaming · Oct 08, 2015 at 03:37 AM

Thank you so much. :)

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

36 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

Related Questions

Building for android fails after adding Unity Ads? 0 Answers

AR Build errors 0 Answers

ArgumentException: Requested value 'X86' was not found. Unity 2019.4.1f1 0 Answers

Can't build apk getting error 0 Answers

IndexOutOfRangeException: IndexOutOfRangeException: Array index is out of range. FollowTarget.Update () (at Assets/Scripts/FollowTarget.cs:29) 1 Answer


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