• 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
0
Question by Fermak · Sep 09, 2017 at 04:53 AM · velocitygetcomponent

Velocity returning 0

So I am trying to spawn a bullet from my player when I click the mouse. Everything is working fine except the velocity.

 bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(10, 10);
 Debug.Log(bullet.GetComponent<Rigidbody2D>().velocity);
 

for some reason the debug is showing the velocity at 0,0 even though literally the line before I am setting it 10,10. Am I missing something about the GetComponent function.

As a side note if I make another script that sets velocity to 10,10 on start and apply it to the bullet prefab it works properly, so the rigidbody is working and there is no collision problems.

Comment
Add comment · Show 3
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 Fermak · Sep 09, 2017 at 06:26 AM 0
Share

So, when I create a reference to the instantiated object and then alter the velocity of that reference after the instantiation it works. Can someone explain why trying to alter the vector prior to instantiation cause the vector to be zero? Is it because it doesn't exist it can't have a vector?

Show more comments

3 Replies

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

Answer by Kishotta · Sep 09, 2017 at 05:46 AM

You need to specify which component you are getting from your bullet.

 // assuming bullet is of type GameObject
 bullet.GetComponent<Rigidbody>().velocity = new Vector2 (10, 10);
 Debug.Log (bullet.GetComponent<Rigidbody>().velocity);
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 Fermak · Sep 09, 2017 at 06:14 AM 0
Share

Oh, wow, I didn't even notice that it deleted that when I copy and pasted, that is weird. Sorry, I am specifying it exactly like that.

avatar image Kishotta Fermak · Sep 09, 2017 at 06:21 AM 0
Share

Where in your script(s) is that code located?

avatar image
0

Answer by Pangamini · Sep 09, 2017 at 03:49 PM

Is your Rigidbody set as dynamic? Does it have mass? Maybe check those. Also don't call GetComponent() twice, it's relatively slow; Call it once, save it in a variable and use that.

Comment
Add comment · Show 1 · 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 Fermak · Sep 09, 2017 at 06:37 PM 0
Share

It is dynamic and it has a mass of 1 with no gravity and no friction. Thanks for letting me know it is slow though!

avatar image
0

Answer by beniciodomi · Jul 31, 2020 at 01:37 AM

Hello! I'm having the same issue: When i check the speed of a rigidbody on the inspector, in some cases, it's moving but the speed remains in zero. In some cases it depends of the surface on wich my object is, but in other cases the rigidbody is simply moving but his speed value is zero.

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.

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

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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Getting Component from javascript to C# 2 Answers

Neater way to test an Enum in a different script? 1 Answer

Problem calling variable across scripts (Closed) 1 Answer

i have variable in camera controller script which is target :transform i want it to change as i click on objects.and be that the same gameobject's transform i clicked as target.what do i do? 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