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
0
Question by Toxic Blob · Mar 10, 2011 at 08:06 PM · javascriptoperatorcomparisonoutofbounds

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;
Comment
Add comment · Show 3
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 Bunny83 · Mar 10, 2011 at 08:58 PM 0
Share

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.

avatar image Toxic Blob · Mar 10, 2011 at 09:51 PM 0
Share

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)

avatar image Toxic Blob · Mar 10, 2011 at 09:52 PM 0
Share

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

1 Reply

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

Answer by loopyllama · Mar 10, 2011 at 10:08 PM

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.

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 Toxic Blob · Mar 10, 2011 at 10:40 PM 0
Share

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

avatar image Bunny83 · Mar 10, 2011 at 11:25 PM 0
Share

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

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

No one has followed this question yet.

Related Questions

How can I compare directions of objects 2 Answers

Having random operators in an equation? 1 Answer

question mark in "setPixel" documentation example? 1 Answer

Shorthand for checking for multiple if's..... 2 Answers

Overload Operator in UnityScript? 0 Answers


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