Jeffrey Grueneberg
Jeffrey Grueneberg

Reputation: 33

Adding force to a rigidbody not working. Error: UnityEngine.Component does not contain a definition for AddForce and no extension method for AddForce

Here is my code:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    public float moveSpeed;
    public Vector3 input;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis("Vertical"));
        Rigidbody2D myRigidBody = rigidbody.AddForce (input * moveSpeed);

    }
}

I am following a tutorial shown [here](https://www.youtube.com/watch?v=qwuPiaFU37w&list=PL0MyxI8F9YPGu6Nxjyv0smaaquNIPp4I-&index=1] and around 27:00 he says that I don't need to reference it as it is built into Unity. I tried adding a reference and couldn't seem to get it to work either. I have tried seeing if I messed up anything with names or checkboxes etc. in the video and nothing seems to be wrong, where could I have messed up and how would I fix it?

I am using Windows 7 and 64bit Unity.

Upvotes: 2

Views: 6880

Answers (1)

YNK
YNK

Reputation: 869

Are you using Unity5?

APIs changed in Unity5... for example to access the rigid body component now you have to use GetComponent<Rigidbody>()

http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/

Upvotes: 4

Related Questions