Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Error CS0120: Object reference is required to access non-static member...

Discussion in 'Scripting' started by Valda-Vaux, Jun 16, 2015.

  1. Valda-Vaux

    Valda-Vaux

    Joined:
    Jan 6, 2015
    Posts:
    198
    Hi guys. I'm following a youtube tutorial and I tried using his script to move an object, but I get an error saying:

    Assets/movement.cs(18,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'

    Here is the script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movement : MonoBehaviour {
    5.     public float moveSpeed;
    6.     private float maxSpeed = 5f;
    7.  
    8.     private Vector3 input;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    18.         Rigidbody.AddForce(input);
    19.    
    20.     }
    21. }
    22.  
    Help?
     
  2. Ecocide

    Ecocide

    Joined:
    Aug 4, 2011
    Posts:
    292
    This should help you:
    http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    You can only call AddForce() on a concrete rigidbody object.

    For example declare a public variable on top of the script

    Code (csharp):
    1.  public Rigidbody rb;
    and in the Update method call:

    Code (csharp):
    1.  rb.AddForce(input);
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    pre unity 5 you could use

    Code (csharp):
    1.  
    2. rigidbody
    3.  
    as a shortcut accessor to the Rigidbody on the gameobject
    with unity 5 they removed the majority of these short cut accessors.

    note the use of lowercase and uppercase here, uppercase is the class, lowercase is the variable


    if you try

    Code (csharp):
    1.  
    2. Rigidbody.someFunction();
    3.  
    you are trying to call a static class level function.

    if you try
    Code (csharp):
    1.  
    2. rigidbody.someFunction();
    3.  
    you are calling a function on the specific instance referred to by the variable "rigidbody" (which you need to declare and initilise manually since 5)
     
  4. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    As was said above me, you need to store the rigidbody that is attached to the object in a variable.

    Code (CSharp):
    1. public class Movement : MonoBehaviour {
    2.     private Rigidbody _rigidbody;
    3.  
    4.     void Start() {
    5.         this._rigidbody = GetComponent<Rigidbody>();
    6.     }
    7.  
    8.     void Update() {
    9.         //Do whatever you'd like with the rigidbody
    10.         this._rigidbody.AddForce(new Vector3(2.0f, 0.0f, 0.0f));
    11.     }
    12. }
     
  5. Valda-Vaux

    Valda-Vaux

    Joined:
    Jan 6, 2015
    Posts:
    198
    Yea, the rigidbody component wasn't added to the object, and apparently I used the old notation that didn't apply for Unity 5. Thank you!