# Experiencing Multiple Issues with Code Possibly Caused By Outdated Tutorial [closed]

As somebody very new to Unity5, I was learning the first steps through a video tutorial and finally got to the coding part of everything. However, subsequent to completion of learning coding, it seems to be having difficulties with the code I typed. Before posting here, I checked for any differences that might be the cause, but after a few look-overs, I couldn't find the problem. I've been having this problem with the Personal Edition of Unity. I've been following along with the DigitalTutors tutorial, "Exploring the 2D Features in Unity". Unfortunately, I had some errors, such as Unity telling me that 'rigidbody2D' was obsolete. Following changing it to 'Rigidbody2D', I got the following errors which stumps me.

I think it also might be worth mentioning that I took some mild liberties with the assets. Some of the assets used in the original tutorial were replaced with some of my own I created. I gave the animations the same names and coded it all the same, but there were definitely a few cosmetic changes I made.

Below, I've done my best to format the errors.

1. Assets/Scripts/MovementController.cs(23,73): error CS0120: An object reference is required to access non-static memberUnityEngine.Rigidbody2D.velocity
2. Assets/Scripts/MovementController.cs(23,84): error CS1502: The best overloaded method match forUnityEngine.Vector2.Vector2(float, float)' has some invalid arguments
3. Assets/Scripts/MovementController.cs(23,84): error CS1503: Argument#2' cannot convert object' expression to typefloat'
4. Assets/Scripts/MovementController.cs(23,21): error CS0120: An object reference is required to access non-static memberUnityEngine.Rigidbody2D.velocity'

I was hoping that someone here might have a clue as to how I can properly revise the code.

I have the code I'm using below:

using UnityEngine;
using System.Collections;

public class MovementController : MonoBehaviour
{
public float maxSpeed = 10.0f;
bool facingRight = true;

Animator anim;

// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}

// Update is called once per frame
void FixedUpdate ()
{
float move = Input.GetAxis("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs(move));
Rigidbody2D.velocity = new Vector2(move * maxSpeed, Rigidbody2D.velocity.y);

if (move > 0 && !facingRight)
{
FlipFacing();
}
else if(move < 0 && facingRight)
{
FlipFacing();
}
}

void FlipFacing()
{
facingRight = !facingRight;
Vector3 charScale = transform.localScale;
charScale.x *= -1;
transform.localScale = charScale;
}
}


I'd prefer not to send the Unity Project, but if it's needed I can send it in a .zip over Mediafire or something similar.

To be blunt :

Rigidbody2D is a type. A type (except if a static class) needs to be instanciated so that you can reference an objet of this type.

For example if you declare a "class A" you will need to call 'A myObject = new A();'

The good way to get the rigidbody from an object is this way :

Rigidbody2D myRigidbody = GetComponent<Rigidbody2D>()


On start you yould store the rigidbody2D in a variable next to the animator. And set it the way showed higher. You can then call it in your code this way.

It should look more like something like that :

using UnityEngine;
using System.Collections;

public class MovementController : MonoBehaviour
{
public float maxSpeed = 10.0f;
bool facingRight = true;

Animator anim;
Rigidbody2D myRigidbody;

// Use this for initialization
void Start ()
{
myRigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}

void FixedUpdate ()
{
float move = Input.GetAxis("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs(move));
myRigidbody.velocity = new Vector2(move * maxSpeed, myRigidbody.velocity.y);

if (move > 0 && !facingRight)
{
FlipFacing();
}
else if(move < 0 && facingRight)
{
FlipFacing();
}
}

void FlipFacing()
{
facingRight = !facingRight;
Vector3 charScale = transform.localScale;
charScale.x *= -1;
transform.localScale = charScale;
}
}