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 /
This question was closed May 17, 2013 at 04:26 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Pultz · May 16, 2013 at 05:43 PM · c#javascriptrigidbody

JS to C# Need some help.

 if(ammo > 0)
             {
             
             
         ammo --;
         var projectile = Instantiate(shot, transform.position, Quaternion.identity);
         projectile.rigidbody.AddForce(transform.forward * 1500);
         }

JS code

 if(ammo > 0)
             {
             
             
         ammo --;
         var projectile = Instantiate(shot, transform.position, Quaternion.identity);
         projectile.rigidbody.AddForce(transform.forward * 1500);
         }

C# code

why does it not work?

theres something wrong with the rigidbody in c#

Comment
Add comment · Show 5
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 hoy_smallfry · May 16, 2013 at 05:45 PM 0
Share

Ins$$anonymous$$d of saying, "theres something wrong", can you show us or describe it in words? What you've glossed over be something crucial to our understanding of the problem.

Also, we may have to see more of your code to deter$$anonymous$$e what's wrong with it. With just a fragment, we can not test it ourselves if some of us want to. In fact, it may be that the code you've given isn't the cause at all, just the code that gets affected.

avatar image ExTheSea · May 16, 2013 at 06:37 PM 0
Share

I think it's the part with the var. I normaly use JS but i think it should be:

  GameObject projectile = Instantiate(shot, transform.position, Quaternion.identity);

Does that work?

Btw. I still agree with what jbarba_ballytech said. Next time please try to explain your problem in more detail then just saying "something is wrong".

avatar image Pultz · May 16, 2013 at 06:45 PM 0
Share

tryed that and just got me this error

Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

avatar image ExTheSea · May 16, 2013 at 06:51 PM 0
Share

Yes I actually did like i see now in fafase's answer. Like i said I don't do much in C#.

avatar image fafase · May 16, 2013 at 06:53 PM 0
Share

Fact is, UnityScript does the cast for you, that is why it does not show and work in Us codes but C# is way much more type safe and needs type to be explicitly defined to avoid surprise.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by fafase · May 16, 2013 at 06:49 PM

The reason is that in C# Instantiate returns an object of type Object, the top class of all. It is required to cast the object to the appropriate type:

 Type t = (Type) Instantiate();

In your case:

 GameObject projectile = (GameObject)Instantiate(shot, transform.position, Quaternion.identity);
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 Pultz · May 16, 2013 at 07:02 PM 0
Share

Thank you very much :)

avatar image fafase · May 17, 2013 at 04:26 AM 0
Share

By the way, for some reason I cannot deter$$anonymous$$e, you are able to cast to GameObject or Transform. But trying to cast to Rigidbody for instance is not working. I reckon it could be due to some internal functioning based on the fact that all object have a GameObject and Transform but not necessarily a Rigidbody.

Follow this Question

Answers Answers and Comments

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do you climb a ladder 1 Answer

Object stops but then eventually breaks through 1 Answer

Tutorial level 2 Answers

this script in Javascript?? 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