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

Simple code that has worked before, now giving error.

Discussion in 'Scripting' started by CurzGuy, Apr 7, 2015.

  1. CurzGuy

    CurzGuy

    Joined:
    Apr 7, 2015
    Posts:
    1
    This is from a beginners tutorial. It worked about a year ago. I stopped using Unity for awhile and needed a refresher, but this code is not working.

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector 3 (moveHorizontal, 0, moveVertical);

    rigidbody.AddForce(movement);
    }
    }

    at Rigidbody.AddForce(movement); "An object reference is required for non static field, method, or property.

    I was wondering if because I am using a new version than I used to use that something has changed. I know this very simple code has worked in the past. I am also copying it directly from the tutorial.

    Any help would be appreciated.
     
  2. mcapousek

    mcapousek

    Joined:
    Jan 11, 2013
    Posts:
    9
    Replace 'RigidBody.AddForce()' by 'GetComponent<RigidBody>.AddForce()'. But this call isn't free so I would cache this reference (result of 'GetComponent' call) and call 'AddForce' directly on it.
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    What he said.

    "Rigidbody", and a lot of other things are no longer automatically referenced, because it's inefficient.

    If you want it to act exactly like the old method that you had, you would use

    Code (csharp):
    1. GetComponent<Rigidbody>().AddForce(movement);
    That is exactly what it was doing in the old version, but you didn't see it. It's inefficient because you should rather fetch the reference only once if you can, as opposed to fetching it every time you want to do something with it.

    The correct way is to do this
    Code (csharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.  
    4. private Rigidbody RigidbodyComponent;
    5.  
    6. void Start()
    7. {
    8.     RigidbodyComponent = GetComponent<Rigidbody>();
    9. }
    10.  
    11. void FixedUpdate()
    12. {
    13. float moveHorizontal = Input.GetAxis("Horizontal");
    14. float moveVertical = Input.GetAxis("Vertical");
    15.  
    16. Vector3 movement = new Vector 3 (moveHorizontal, 0, moveVertical);
    17.  
    18. RigidbodyComponent.AddForce(movement);
    19. }
    20. }