x


Ternary operator fails, but verbose comparison doesnt?

Trying to write neat & compact code Im confused as to why in UnityScript this fails with error BCE0022: Cannot convert 'Object' to UnityEngine.Vector2...

var useTouch : boolean = true;
position = useTouch ? Input.touches[0].position : Input.mousePosition;

...yet the following is fine. Are they not essentially the same?

var useTouch : boolean = true;
if( useTouch )
    position = Input.touches[index].position;
else
    position = Input.mousePosition;
more ▼

asked Mar 10 '11 at 08:06 PM

Toxic Blob gravatar image

Toxic Blob
656 18 20 33

I just tried different versions but almost every one worked and i never get this error. Are you sure you get that error on the ternary operator line? Is position defined above or is it implicitly created here? Another thing: You just access touched[0] but if there is no touch at all you will get an index out of bounds error because the touched array will be empty. Without the code above and/or below i can't see anything else wrong.

Mar 10 '11 at 08:58 PM Bunny83

Positive I get it on this line. I comment it out, everything works, uncomment, fail. position is defined elsewhere, and used elsewhere fine. Im also checking that there are touches (note, Im not getting an out of bounds error, but the Object to Vector2 casting error)

Mar 10 '11 at 09:51 PM Toxic Blob

Im also using #pragma strict, but it fails without as well.

Mar 10 '11 at 09:52 PM Toxic Blob
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Input.touches[o].position is a vector2 and Input.mousePosition is a vector3. You are using javascript which, for better or worse, allows you to not declare what type position is. in your second working example it is clear that one case position is a vector2 and in another case it is a vector3. In your failing example you are saying if this is true, position is a vector2, and if it false it is a vector3, but it happens at runtime. At compile time which is it? it cannot be both a vector2 and vector3 so it fails. Try explicitly casting the vector2 to a vector3 in your first example and that should work..like Vector3(Input.touches[0].position.x,Input.touches[0].position.y,0.0F)...or maybe even (Vector3)Input.touches[0].position would work, you'd have to try and see.

more ▼

answered Mar 10 '11 at 10:08 PM

loopyllama gravatar image

loopyllama
1.5k 1 4 18

Perfect. Vector2(Input.mousePosition.x, Input.mousePosition.y) works as well, and its a bit less code. Thanks!

Mar 10 '11 at 10:40 PM Toxic Blob

Well, i don't develop for iphone so i usually don't use Input.touches ;). Good to know that the position of a touch is a Vector2. +1

Mar 10 '11 at 11:25 PM Bunny83
(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:

x3452
x38
x28
x12
x2

asked: Mar 10 '11 at 08:06 PM

Seen: 1034 times

Last Updated: Mar 10 '11 at 08:06 PM