Search code examples
c#unity-game-engine

How to use the rigid body method at Unity 5.1.2?


using UnityEngine;
using System.Collections;
public class RigidBodyScript : MonoBehaviour {
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKey (KeyCode.UpArrow)) {
            this.transform.rigidbody.AddForce() (
                Vector3.forward * 300 * Time.deltaTime);
        }
    }
}

I read a book and try to use the transform.rigidbody.AddForce() method.
But the unity version of book is previous, so at my unity program that method make error at rigid body.
At Unity 5.1.2, how to use this method?


Solution

  • The rigidbody property is deprecated in recent versions of Unity. So accessing them with a transform.rigibody won't work any more.

    You will have to get the Rigidbody component with a GetComponent<Rigidbody>() call now (or one of the other GetComponent varieties).

    This is what Unity is trying to correct for you. It can't however, given that the code you have written won't compile in the first place. For example

    this.transform.rigidbody.AddForce() (Vector3.forward * 300 * Time.deltaTime);
    

    contains too many braces.

    So that is what it's telling you: "I'm trying to upgrade your code, but I can't".