Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Game_Smith_legacy · Aug 17, 2011 at 07:43 AM · translationcasting

Problem with type casting from C# to JS

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.

Comment
Add comment · Show 2
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 Game_Smith_legacy · Aug 17, 2011 at 08:35 AM 0
Share

@NOAA_Julien

That fixed the compiler error, but the code does not function the same as it did.

Before in the C# version, the bullet would fire correctly out of the cannon. But now when it fires, the bullet drops at it's spawn point ins$$anonymous$$d of moving along it's trajectory.

I'm not for sure if this is caused by the differences in how Unity handles the two scripting languages or not, I'm not familiar enough with both of them to be sure.

avatar image Game_Smith_legacy · Aug 18, 2011 at 07:08 AM 0
Share

@$$anonymous$$ohsenEbrahimi

You suggestion worked as well, but it will only let me mark one answer as correct.

3 Replies

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

Answer by Julien-Lynge · Aug 17, 2011 at 08:07 AM

I don't code in JS, so forgive me if there's a more elegant solution.

I poked around and it appears from my testing like JS returns a GameObject (rather than Object like C#) on Instantiate calls. So, your error seems to be that Unity doesn't want to typecast a GameObject into a Transform. What I did to get it working is treat it as a GameObject and grab the associated transform:

 var bullet : Transform = (Instantiate(bulletPrefab, startPos, shotAngle)).transform;

Good luck.

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 Julien-Lynge · Aug 17, 2011 at 08:11 AM 0
Share

Also, this might enlighten you:

http://forum.unity3d.com/threads/3276-Typecasting-in-Javascript

avatar image Joshua · Aug 17, 2011 at 10:05 PM 0
Share

The outer ()'s can be left out. But other then that this should work.

avatar image
-1

Answer by rushikesh90 · Aug 17, 2011 at 08:37 AM

use transform instead of Transform first, since JS is case sensitive language. Again it will be better if you instantiate the tansform object inside a function if you have to create it at runtime(though I dont know where exactly you are declaring variable.)

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 rushikesh90 · Aug 17, 2011 at 10:02 AM 0
Share

If you cannot get answer from my post it do not suits you to unknowingly mark it wrong, bear it in $$anonymous$$d.

avatar image Game_Smith_legacy · Aug 18, 2011 at 06:13 AM 0
Share

I'm not the one who marked it wrong.

avatar image
0

Answer by MohsenEbrahimi · Aug 17, 2011 at 11:49 AM

Removing "as Transform" from first statement can solve your problem.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error, class project 1 Answer

Vector-based movement? 1 Answer

JS to C# Translation? 2 Answers

Having a problem translating part of a script which uses C# delegates into JS 0 Answers

Casting Object to Material 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