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
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.
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.
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/
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;
}
}
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
Okay so I just added the corrections that you put in and unfortunately it didn't fix it but thank you for trying
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
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?
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.
I tried using the search bar and both those results did not resolve the issue. But thank you
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
Follow this Question
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