Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

New to Coding with c#

Discussion in 'Scripting' started by Synder, Apr 15, 2015.

  1. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    I'm trying to complete one of the tutorial projects and have been following it to a 'T' but when I went to incorporate my code into the sphere I'd created it says there is a problem in the code and I can't figure out how to fix it. Now I'm sure it's something super simple but being I have very little experience as to what I'm doing I could use a pointer as to how to fix this problem.

    This is what it is telling me when I debug...

    An object reference is required for the non-static field, method, or property 'UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3)'


    using UnityEngine;
    using System.Collections;

    public class Player_Controller : MonoBehaviour
    {
    void FixedUpdate (){
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    Rigidbody.AddForce (movement);
    }
    }
     
  2. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
  3. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You can't call RigidBody.AddForce directly because AddForce is a function in the RigidBody class, you need to create a variable/object of type RigidBody and call AddForce through the variable. For example this script (this assumes the rigidbody is on the same game object as this script)

    Code (csharp):
    1.  
    2. Rigidbody rb; //first create a variable of the desired type, in this case RigidBody
    3.  
    4. void Start()
    5. {
    6.     rb = gameObject.GetComponent<RigidBody>(); //intialize the variable
    7. }
    8.  
    9. void FixedUpdate()
    10. {
    11.     float moveHorizontal = Input.GetAxis ("Horizontal");
    12.     float moveVertical = Input.GetAxis ("Vertical");
    13.  
    14.     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    15.     rb.AddForce(movement); //call the function through the variable
    16. }
    17.  
    18.  
    So every time you get the message that an object reference is required, you need to make an object (variable of a certain type) and access whatever you're trying to access through the variable/object.
     
    Flickayy likes this.
  4. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
    The error is stating that the 'Rigidbody.AddForce' is being called as a static method, but AddForce needs to be called from an Rigidbody object instance.

    Something like:
    Code (CSharp):
    1. // We need a Rigidbody instance to work with.
    2. // Assign rb to the current game object's Rigid Body component
    3. // (make sure the game object has a rigid body component before running)
    4. Rigidbody rb = GetComponent<Rigidbody>();
    5. rb.Rigidbody.AddForce(movement);
    Replace your original AddForce method call with these lines.

    [Edit]
    @willemsenzo beat me to it. Do it his way, as it caches the rigid body at the start, while my simple to read way gets the rigid body reference each frame, costing a bit more time per frame.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In unity 4.x you could use rigidbody (small r) to access the attatched Rigidbody. From 5.0 onwards you must use GetComponet<Rigidbody>() instead.

    Proper case is a big issue.
     
  6. Synder

    Synder

    Joined:
    Apr 15, 2015
    Posts:
    15
    Thank you guys so much, you're all awesome. It works.