I’ve been fighting this problem for a couple of hours and have finally decided to ask my first question on UA.
I have a line of code that works fine in C#
Transform bullet = (Transform)Instantiate(bulletPrefab, startPos, shotAngle);
But when I try and translate it to JS I keep getting errors. I’ve tried a couple of different ways of writing this line, inlcuding:
var bullet : Transform = Instantiate(bulletPrefab, startPos, shotAngle) as Transform;
and
var bullet : Transform = (Transform)Instantiate(bulletPrefab, startPos, shotAngle);
But neither way will compile for me. I keep getting the error “UCE001: ‘;’ expected. Insert a semicolon at the end.” for this line of code.
I can not for the life of me figure out what simple mistake I am making in this translation. I have not coded much in C# (at least inside of Unity anyways), I’ve mainly used JS (but never outside of Unity). So I know it has to be something simple, so any help would be greatly appreciated.
EDIT - Well I feel dumb, after some sleep, I went through my code again and found out that there was a small bug elsewhere in the code that was the actual cause of the ball not to follow it’s trajectory.
All of the following versions of code work now:
var bullet : Transform = Instantiate(bulletPrefab, startPos, shotAngle) as Transform;
var bullet : Transform = Instantiate(bulletPrefab, startPos, shotAngle);
var bullet : Transform = (Instantiate(bulletPrefab, startPos, shotAngle)).transform;
But I did learn a lot about casting in JS. Thanks everyone for there help, and for putting up with my dumb 1st question.
Also, this might enlighten you: http://forum.unity3d.com/threads/3276-Typecasting-in-Javascript
– Julien-LyngeThe outer ()'s can be left out. But other then that this should work.
– anon73820239