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 jderryberry25 · Feb 10, 2017 at 09:24 PM · unity5

CS0120 OBJECT REQUIRED

I'm working on a robot controller and is giving me error cs0120 an object reference is required for non-static field, method, or property rigidbody2d. Velocity. Every correction to rigidbody2d gives me a velocity error every correction to Velocity gives me an entire script error alt text

20170210-171158.jpg (293.9 kB)
Comment
Add comment · Show 9
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 HenryStrattonFW · Feb 10, 2017 at 09:51 PM 0
Share

You will be more likely to get help if you provide the script that is giving you errors, that way people can check it for the cause of the errors. I advise adding it by editing the post as preferred to posting it as a comment as its easier for new viewers to see.

avatar image jderryberry25 · Feb 11, 2017 at 12:13 AM 0
Share

Photo was added of script

avatar image HenryStrattonFW jderryberry25 · Feb 11, 2017 at 11:28 AM 1
Share

Based on your photo @FortisVenaliter is entirely correct and his answer should be accepted as such. You are attempting to access an instance member variable (velocity) but you are trying to access it via a type "RigidBody2D" which you cannot do. You can tell its being read as a type ins$$anonymous$$d of an instance since it is coloured as such in your code. You need to get an instance of a rigidbody2D in order to be able to access the non-static variables.

The two links provided by Fortis provide all the information you would need, but if you are struggling, feel free to paste the entire script and I'll show you what to change.

avatar image jderryberry25 · Feb 11, 2017 at 03:15 PM 0
Share

The entire script I'm using is on this page https://www.instructables.com/id/$$anonymous$$ake-A-2D-Infinite-Runner-with-Unity/step6/The-Robot-Controller-Script/

avatar image HenryStrattonFW jderryberry25 · Feb 11, 2017 at 03:22 PM 2
Share

Ok so the issue here is that this script is likely from an older version of unity, in which rigidbody2D (notice the lowercase r not uppercase R) was a property that you could access on a monobehaviour. This is no longer the case as such properties have been deprecated and should be avoided. I've updated this script to store a reference or type RigidBody2D which is then set using a GetComponent in the Start method. the rest of the code then utilises this variable.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RobotController : $$anonymous$$onoBehaviour
 {
     //This will be our maximum speed as we will always be multiplying by 1
     public float maxSpeed = 2f;
     //a boolean value to represent whether we are facing left or not
     bool facingLeft = true;
     //a value to represent our Animator
     Animator anim;
 
     // This will hold the reference to our rigidbody2d component.
     private Rigidbody2D myRigidBody;
 
 
     // Use this for initialization
     void Start()
     {
         //set anim to our animator
         anim = GetComponent<Animator>();
         // get the rigid body component.
         myRigidBody = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         float move = Input.GetAxis("Horizontal");//Gives us of one if we are moving via the arrow keys
                                                  //move our Players rigidbody
         myRigidBody.velocity = new Vector3(move * maxSpeed, myRigidBody.velocity.y);
         //set our speed
         anim.SetFloat("Speed", $$anonymous$$athf.Abs(move));
         //if we are moving left but not facing left flip, and vice versa
         if (move < 0 && !facingLeft)
         {
 
             Flip();
         }
         else if (move > 0 && facingLeft)
         {
             Flip();
         }
     }
 
     //flip if needed
     void Flip()
     {
         facingLeft = !facingLeft;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }
avatar image jderryberry25 HenryStrattonFW · Feb 11, 2017 at 05:42 PM 0
Share

Okay so the strip that you gave me was correct I just typed it incorrectly in the script editor but I have to scrap the whole thing because now that the script is correct on the character still doesn't move though and I think that's just because this was based on 3. Whatever unity not 5.5

avatar image jderryberry25 · Feb 11, 2017 at 04:08 PM 0
Share

Okay so I just added the corrections that you put in and unfortunately it didn't fix it but thank you for trying

avatar image jderryberry25 · Feb 11, 2017 at 04:11 PM 0
Share

Henry I don't think this is a script error because I'm going over the information popping up when I looking for Corrections that I can make and it's telling me to add a velocity to the rigid body and add a force to the rigid body for the two errors that I have so I think I need to add that in unity and not in the script

avatar image HenryStrattonFW jderryberry25 · Feb 11, 2017 at 04:40 PM 1
Share

Does the object that this script is placed on have a RigidBody2D component on it? If not then that would account for some runtime null ref errors. I assumed the tutorial had already had you put that on the object, What are the errors you are getting now, are they compile errors or runtime?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by FortisVenaliter · Feb 10, 2017 at 09:57 PM

First result from google for that error code describes the issue from the developer of the language. The second result is from one of the many similar questions on this very site that explain the issue and how to fix it. The search bar is your friend.

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 jderryberry25 · Feb 10, 2017 at 10:08 PM 0
Share

I tried using the search bar and both those results did not resolve the issue. But thank you

avatar image FortisVenaliter jderryberry25 · Feb 10, 2017 at 10:45 PM 1
Share

Yes... yes, they do. You're referencing the class name, but you need to reference an instance.

If you believe you have an anomaly where that isn't the case, post your code so we can see what the problem is.

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

100 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 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

What are the property names in the new Unity 5 standard shader 3 Answers

Scripting errors in Unity Ads Import 1 Answer

How to access a live website like google inside the Unity3D scene (gameobject) 2 Answers

Why can I only use unity in Offline mode? 0 Answers

Unity not opening on Ubuntu 15.10 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