• 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
Question by MrSplosion · Dec 10, 2010 at 10:57 PM · errorsyntax-errorbce0023

Broken Script Help

Hello I'm a little new to Unity im trying to make my first Unity based project but the console says "No appropriate version of 'UnityEngine.Rigidbody.AddForce' for the argument list '(float)' was found." I'm just trying to add force to my bullet to make it actually shoot. Here script:

function Update(){
if(Input.GetKeyDown("space")){
    Instantiate(bullet, GameObject.Find("Bullet Spawn").transform.position, Quaternion.identity);
    bullet.rigidbody.AddForce(transform.position.z * 2000);
    print("shot");
}

}

Thanks!

Comment
Statement
JesusChristChangedMe

People who like this

2 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Statement · Dec 10, 2010 at 11:03 PM

No appropriate version of 'UnityEngine.Rigidbody.AddForce' for the argument list '(float)' was found.

bullet.rigidbody.AddForce(transform.position.z * 2000);

You are accessing the z component of position, which is a float value. I guess what you really want to do is get the forward direction of the transform. Also, you're trying to add the force to what looks to be the prefab. You should add the force to the new clone you instantiated.

var power = 2000;
var position = GameObject.Find("Bullet Spawn").transform.position;
var clone = Instantiate(bullet, position, Quaternion.identity);
clone.rigidbody.AddForce(transform.forward * power);

However, you might want to use another force mode since you're "shooting" the object away. Impulse force mode sounds appropriate.

This mode is useful for applying forces that happen instantly, such as forces from explosions or collisions.

var power = 2000;
var position = GameObject.Find("Bullet Spawn").transform.position;
var clone = Instantiate(bullet, position, Quaternion.identity);
clone.rigidbody.AddForce(transform.forward * power, ForceMode.Impulse);

You probably want to tweak power a bit. 2000 sounds a lot.

Comment
MrSplosion
Eric5h5
JesusChristChangedMe

People who like this

3 Show 7 · 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 MrSplosion · Dec 10, 2010 at 11:11 PM 0
Share

Thanks but I'v tried that and nothing happens the bullet just falls with no force at all.

avatar image Statement · Dec 10, 2010 at 11:36 PM 1
Share

I have updated my answer. We were trying to add the force on the prefab rather on the new bullet. :)

avatar image MrSplosion · Dec 11, 2010 at 03:52 AM 0
Share

Oh ok I see thanks

avatar image MrSplosion · Dec 14, 2010 at 01:03 AM 0
Share

Ok so now when I shoot my bullet shoots on the wrong axis. I replaced AddForce(transform.forward... with AddForce(transform.position.x... and I get an error :( why doesnt that work?

avatar image Statement · Dec 15, 2010 at 01:40 AM 1
Share

It sounds like your character isn't aligned with the face pointing forward. transform.forward is just the forward direction (z axis, the the blue arrow) of your character. If your character is pointing another direction, use that direction. You can try transform.right or transform.up also, as well as trying to invert the direction with - (negate sign). Basically you just need the direction you're facing. You can get this direction from helper objects positioned to align the bullet if you have awkward angles but I doubt that is necessary.

Show more comments

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

No one has followed this question yet.

Related Questions

Script error: No appropriate version of 'UnityEngine.Transform.TransformDirection' for the argument list '(System.Type)' was found 2 Answers

script gets this error: (6,20): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, System.Object)' was found. 2 Answers

sound on button press error + its firing when space is pressed 1 Answer

How "include" the Raycast?! 1 Answer

Unexpected Token: Collider. 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