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

Super Noob Question

Discussion in 'Scripting' started by Damocles346, Mar 16, 2015.

  1. Damocles346

    Damocles346

    Joined:
    Mar 16, 2015
    Posts:
    2
    I am doing the tutorial for the Roll-A-Ball game. I am on the "Move the Player" step, and the code that the tutorial has you typing out doesn't seem to work.

    Here is a copy of the code that I have typed out myself:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     void FixedUpdate ()
    7.     {
    8.         float moveHorizontal = Input.GetAxis ("Horizontal");
    9.         float moveVertical = Input.GetAxis ("Vertical");
    10.  
    11.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    12.  
    13.         Rigidbody.AddForce(movement);
    14.  
    15.     }
    16. }
    And here is a copy of the error that it is throwing: Assets/Scripts/PlayerController.cs(13,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'

    It's having an issue with the Rigidbody.AddForce, and I can't seem to get it to resolve. I have done some research, but as I am a new to doing any of this, it's all a bit above my head at the moment. L'il help?

    Thanks much.
     
  2. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    You are probaply using unity 5

    in Unity 4 a GameObject could access its rigidbody component directly with rigidbody.AddForce()

    in Unity 5 rigidbodys are access in the same way like every other Component with the GetComponent<Rigidbody>().AddForce(...)
    GetComponent<MyScript>().DoThis();

    the programming structure accessing components is now consistent

    line 13 is wrong.

    replace
    Rigidbody.AddForce(movement);

    with
    Code (CSharp):
    1. this.GetComponent<Rigidbody>().AddForce(movement);
     
    Last edited: Mar 16, 2015
    josker and Kiwasi like this.
  3. Damocles346

    Damocles346

    Joined:
    Mar 16, 2015
    Posts:
    2
    Thanks bro. No idea what I am doing. Baby steps, right? :)
     
  4. Lucky10s

    Lucky10s

    Joined:
    Mar 18, 2015
    Posts:
    1
    Thanks "tobad", as of March 18, 2015 your code works in the roller ball project. Your a life saver considering I just started out.