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 /
  • Help Room /
avatar image
0
Question by MrSideShow · Dec 04, 2015 at 05:25 AM · rigidbody2dvelocitycodepage

rigidbody2D's velocity not working

I'm trying to make a 2d object move around freely but I keep receiving the error with my code: error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity' . I'm not certain what's causing it or how to really fix it, code below if anyone can help me understand why it is my rigid body doesn't want to move.

 using UnityEngine;
 
 /// <summary>
 /// Player controller and behavior
 /// </summary>
 public class PlayerScript : MonoBehaviour
 {
     /// <summary>
     /// 1 - The speed of the ship
     /// </summary>
     public Vector2 speed = new Vector2(50, 50);
     
     // 2 - Store the movement
     private Vector2 movement;
     
     void Update()
     {
         // 3 - Retrieve axis information
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
         
         // 4 - Movement per direction
         movement = new Vector2(
             speed.x * inputX,
             speed.y * inputY);
         
     }
     
     void FixedUpdate()
     {
         // 5 - Move the game object
         Rigidbody2D.velocity = movement;
     }
 }
 

For more information I'm attempting to follow this tutorial that was written for unity 4.3 and I am currently using unity 5 if that helps.

Comment
Add comment
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
1
Best Answer

Answer by MelvMay · Dec 04, 2015 at 07:50 AM

First, you should try using a Google search for 'CS0120' as it comes up with the answer many times, including posts on the Unity site instead of posting here.

Google Search: CS0120

The error tells you exactly what is wrong so I presume you're new to programming? If so, the 'Rigidbody2D' you're using is the type name. The only time you can access properties like this is if they are static; velocity isn't static. You want the instance of the Rigidbody2D component you've presumably added. You use:

 var rb = GetComponent<Rigidbody2D>();

to get the component. You can then access its properties like this

 rb.velocity = movement;
Comment
Add comment · Show 4 · 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 MrSideShow · Dec 04, 2015 at 01:29 PM 0
Share

I am new to program$$anonymous$$g and for some reason didn't think to google the error code so thank you. What is the rb variable though, just the name of it?

avatar image MelvMay ♦♦ MrSideShow · Dec 04, 2015 at 01:57 PM 0
Share

'rb' is the name of the variable I chose that stands for 'rigid-body'; could be anything.

If I were to rewrite your code, I'd do it like so: using UnityEngine;

  /// <summary>
  /// Player controller and behavior
  /// </summary>
  public class PlayerScript : $$anonymous$$onoBehaviour
  {
      /// <summary>
      /// 1 - The speed of the ship
      /// </summary>
      public Vector2 speed = new Vector2(50, 50);
      
      // 2 - Store the movement
      private Vector2 movement;

      private Rigidbody2D body;

      void OnStart()
      {
          body = GetComponent<Rigidbody2D>();
      }

      void Update()
      {
          // 3 - Retrieve axis information
          float inputX = Input.GetAxis("Horizontal");
          float inputY = Input.GetAxis("Vertical");
          
          // 4 - $$anonymous$$ovement per direction
          movement = new Vector2(
              speed.x * inputX,
              speed.y * inputY);
          
      }
      
      void FixedUpdate()
      {
          // 5 - $$anonymous$$ove the game object
          body.velocity = movement;
      }
  }

Hope this helps.

avatar image MrSideShow MelvMay ♦♦ · Dec 05, 2015 at 01:02 AM 0
Share

It does, a lot. So the problem was that there was no rigid body to call on, and you fixed that by first creating the private method(?) of Rigidbody2D and na$$anonymous$$g it body, then when the object is first called on, it retrieves the information for a rigidbody2D. Does that sound right?

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.

Follow this Question

Answers Answers and Comments

35 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

Related Questions

Why is a gameobject moves at start? 0 Answers

how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers

how to clamp( set max value) the velocity of a 2d rigidbody? 1 Answer

How do I make my character move on the Z axis on 2d rigidbody? 0 Answers

How to have the same speed on Rigidbody2D in all resolutoins ?? 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