I need help working out a C# script with error CS0120
I have recently created the following script to make my character object move in either direction and also flip his sprite when moving left if facing right and visa versa
//variables are being set
public float maxspeed = 10f;
bool facingright = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis("horizantal");
UnityEngine.Rigidbody2D.velocity = new Vector2(move * maxspeed, UnityEngine.Rigidbody2D.velocity.y);
if (move > 0 && !facingright)
Flip();
else if (move < 0 && facingright)
Flip();
}
//flips the player when they're facing left
void Flip()
{
facingright = !facingright;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
However i get error CS0120 and I can't figure out how to fix it.
@gooopil To clarify the error is on line 17 of the script, the one that starts with UnityEngine.Rigidbody2D.Velocity
You are trying to modify a property in the class, not on an instance of the class. You need a reference to the instance of Rigidbody you want to modify.
For instance if the script is attached to the same Gameobject you want to move:
Declare a field in your class:
private Rigidbody2D myRigidbody2D;
then in the Start, retrieve the reference:
myRigidbody2D = GetComponent<Rigidbody2D>();
You can then replace your line with
myRigidbody2D.velocity = new Vector2(move * maxspeed, myRigidbody2D.velocity.y);
I'm still getting the same error P.S. I did as you said and here is the new script
//variables are being set
public float maxspeed = 10f;
bool facingright = true;
private Rigidbody2D myRigidbody2D;
// Use this for initialization
void Start () {
myRigidbody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis("horizantal");
myRigidbody2D.velocity = new Vector2(move * maxspeed, UnityEngine.Rigidbody2D.velocity.y);
if (move > 0 && !facingright)
Flip();
else if (move < 0 && facingright)
Flip();
}
//flips the player when they're facing left
void Flip()
{
facingright = !facingright;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
@gooopil please tell me what lines need to be edited and how.
myRigidbody2D.velocity = new Vector2(move * maxspeed, myRigidbody2D.velocity.y);
@gooopil Sorry about the last thing asking for what needed editing because I figured it out
Thank you for you help as I have now got the game to launch without an error message.
Answer by gooopil · Apr 09, 2016 at 08:12 PM
It would be better to ask the question pinpointing where the error is thrown, and indicating the complete error message.
At any rate, this is incorrect:
float move = Input.GetAxis("horizantal");
It should be
float move = Input.GetAxis("Horizontal");
Your answer
Follow this Question
Related Questions
How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers
[HELP] Following the 2D character controller tutorial from the live training section (C#) 0 Answers
How can I load/save a Prefab refference? 0 Answers
Script isn't working. Easy solutions to adding dialogue? Help ASAP - project due in a few days 1 Answer