How to perform Explicit typecasting in UnityScript?

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

more ▼

asked Jul 05, 2012 at 04:00 PM

avatar image

20 6 4 8

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

2 answers: sort voted first

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.

more ▼

answered Jul 05, 2012 at 05:37 PM

avatar image

115k 59 232 729

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

Jul 05, 2012 at 06:20 PM ThePunisher

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'

Jul 05, 2012 at 06:27 PM whydoidoit

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.

Jul 05, 2012 at 06:42 PM whydoidoit

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

Jul 05, 2012 at 06:44 PM ThePunisher

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

Jul 06, 2012 at 10:39 AM arkochelsea
(comments are locked)
10|3000 characters needed characters left
 float myHealth = 10f;
 int healthA = myHealth as int;
 int healthB = (int)myHealth;
more ▼

answered Jul 05, 2012 at 05:36 PM

avatar image

500 38 32 55

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

Jul 05, 2012 at 05:37 PM Eric5h5
(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



Answers and Comments



asked: Jul 05, 2012 at 04:00 PM

Seen: 6951 times

Last Updated: May 10, 2014 at 01:29 PM