• 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
2
Question by arkochelsea · Jul 05, 2012 at 04:00 PM · javascripttypecasting

How to perform Explicit typecasting in UnityScript?

Typecasting as in from Object to GameObject, int to float etc.

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

2 Replies

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

Answer by Eric5h5 · Jul 05, 2012 at 05:37 PM

You can use "as" for reference types.

 var foo : Object;
 var bar = foo as GameObject;

There isn't a direct method for value types, but you can use parseFloat or parseInt in the case of floats and ints.

Comment
Add comment · Show 11 · 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 ThePunisher · Jul 05, 2012 at 06:20 PM 0
Share

To be fair, he said "UnityScript" not Javascript, nor C#, nor Boo. So it wasn't very specific.

avatar image whydoidoit · Jul 05, 2012 at 06:27 PM 1
Share

I guess we all call Javascript Unity Script here to be clear that it isn't actually Javascript. Unity internally compiles to a UnityScript dll when you write 'Javascript'

avatar image whydoidoit · Jul 05, 2012 at 06:42 PM 2
Share

Yes - and given that it makes everyone think it is real Javascript that is a very big shame (given the massive differences). Hence many people specifically call it Unity Script to try to be clear or to raise the question and make the point.

I agree that this is not an ideal situation.

avatar image ThePunisher · Jul 05, 2012 at 06:44 PM 1
Share

Yep. I guess the point I was trying to make was that I didn't know UnityScript meant JavaScript. I'll keep that in $$anonymous$$d next time.

avatar image arkochelsea · Jul 06, 2012 at 10:39 AM 1
Share

I am sorry if i haven't made myself clear to others.

The JavaScript Unity uses is in many ways different from normal JavaScript ( the original language ). So i asked about UnityScript, i.e. Unity's JavaScript. @ThePunisher

Show more comments
avatar image
-2

Answer by nventimiglia · Jul 05, 2012 at 05:36 PM

 float myHealth = 10f;
 int healthA = myHealth as int;
 int healthB = (int)myHealth;
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 Eric5h5 · Jul 05, 2012 at 05:37 PM 3
Share

Bizarrely, the question specifically asks for UnityScript and people post C# answers...not helpful....

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Error with Kimmons dialogue system/ Interface must be public or explicit 1 Answer

Data types and explictly stating 2 Answers

Debugging during runtime 1 Answer

Migrating data from C# to UnityScript 2 Answers

change button texture from different button press 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