Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Jaywalker · Aug 17, 2010 at 03:49 PM · inputtouchexceptionoutofbounds

index out of bounds

Hey,

This is probably a really noob question, but I cannot find the answer. The following code gives:

UnityException: Index out of bounds.

can somebody point me to why I'm failing here:

if ((Input.GetTouch(0).phase == TouchPhase.Began)  && !isPaused )
{
   //Do something
}

Thanks a Lot!

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

6 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Jaywalker · Aug 19, 2010 at 11:44 AM

I solved the problem i was having by assigning the value i wanted (the touch position) to a variable and using that instead.

still puzzled as to why I was getting the error, and not getting it in the following.

if (Input.touchCount > 0)
{
    // Get position of the first finger
    var touchPosition : Vector2 = Input.GetTouch(0).position;
}
Comment
Add comment · Show 2 · 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 Wolfram · Aug 19, 2010 at 02:01 PM 0
Share

Ah, ok, it seems internally touches are stored in some kind of dynamic array. When calling Input.GetTouch(0) you access the first element of that array, but the array apparently is empty when there are no touches. Hence the out-of-bounds. I never worked with iPhone, so I didn't know that. Glad it worked out for you.

avatar image Jaywalker · Aug 21, 2010 at 07:09 AM 0
Share

Cool! that makes sense. If you make an answer of it i can give you the points. Thanks!

avatar image
1

Answer by Wolfram · Aug 17, 2010 at 04:21 PM

Wild guess: Use iPhoneTouch.GetTouch(0), not Input. But the error message would be bizarre if that's the answer.

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 Jaywalker · Aug 19, 2010 at 10:10 AM 0
Share

Hey, Thanks! Didn't work though. The problems seems to occur if the code is not in a for loop.. do you know if this has to be the case?

avatar image
0

Answer by BlackPanda · Mar 15, 2014 at 06:12 AM

I think its because since the Touches are stored in an array, you have to iterate through it (like they have shown in the documentations) according to the touchCounts registered. If not, like Wolfram mentioned, you will be accessing the first element of an empty array.

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 subzeromot · Nov 01, 2015 at 01:28 PM

I can fix this problem by add condition: if(Input.touchCount > 0) I put my code in Update() function so at the beginning, when i have no touch, this code still run, i think the problem is come from there

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 leonknd13 · Sep 22, 2016 at 01:50 PM

a same problem

i fix with GetLenght:

     // Return X or Y Coord of touch - Use Ex: ToqueCoord ('x', i); i as index of touch; 

 int ToqueCoord(char c, int i) {
     int x = 0, y = 0; 
     int t = Input.touches.GetLength (0);

     Debug.Log (t);

     if (t >= (i)) {
         x = (int)Input.touches [i - 1].position.x;
         y = (int)Input.touches [i - 1].position.y;
         Debug.Log ("OK");
     }

     if (c == 'x') {
         return (x);
     }
     if (c == 'y') {
         return (y);
     }
             
     return (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
  • 1
  • 2
  • ›

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Samsung 42" Touch Screen: Touch response issues 1 Answer

Problem with Input.touchCount 0 Answers

How can I use Input touch for this script. 0 Answers

Why can't I press both my touch controls at the same time? 1 Answer

Touch Input help.. 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