x


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!

more ▼

asked Aug 17 '10 at 03:49 PM

Jaywalker gravatar image

Jaywalker
514 15 21 30

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
}
more ▼

answered Aug 19 '10 at 11:44 AM

Jaywalker gravatar image

Jaywalker
514 15 21 30

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.

Aug 19 '10 at 02:01 PM Wolfram

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

Aug 21 '10 at 07:09 AM Jaywalker
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Aug 17 '10 at 04:21 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

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?

Aug 19 '10 at 10:10 AM Jaywalker
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x952
x577
x128
x12
x6

asked: Aug 17 '10 at 03:49 PM

Seen: 3632 times

Last Updated: Aug 17 '10 at 03:49 PM