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. Dismiss Notice

Following tutorial "roll-a-ball" and there are errors

Discussion in 'Scripting' started by Rample Pampkins, Mar 7, 2015.

  1. Rample Pampkins

    Rample Pampkins

    Joined:
    Mar 7, 2015
    Posts:
    2
    I am fallowing the roll a ball tutorial (9:40)and am getting the error below when I try to run the game. I know c++ pretty well and I'm trying to jump right into unity using my general programmer knowledge. I have no previous game engine experience.

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

    code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.     }
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.    
    13.     }
    14.  
    15.     void FixedUpdate(){
    16.         float moveHorizontal = Input.GetAxis ("horizontal");
    17.         float moveVertical = Input.GetAxis ("Vertical");
    18.  
    19.         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
    20.  
    21.         Rigidbody.AddForce (movement);
    22.     }
    23. }
    24.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Shouldn't that be:
    Code (CSharp):
    1.  
    2. rigidbody.AddForce(movement);
    3.  
     
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    or something like this for Unity 5 >>:

    Code (CSharp):
    1. GetComponent<Rigidbody>().AddForce(movement*speed*Time.deltaTime);
     
    Kiwasi likes this.
  4. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    or ..
    Predefine a variable and use it in subsequent Updates.

    Code (CSharp):
    1.     private Rigidbody rb;
    2.  
    3.     void Start ()
    4.     {
    5.         //Declare the rigidbody within the script
    6.         rb = GetComponent<Rigidbody>();
    7.     }
    8.  
     
    Kiwasi likes this.
  5. Rample Pampkins

    Rample Pampkins

    Joined:
    Mar 7, 2015
    Posts:
    2
    Thanks, I think I'm getting closer So I have this now:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     Rigidbody rb;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         rb = GetComponent<Rigidbody>();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     void FixedUpdate(){
    19.         float moveHorizontal = Input.GetAxis ("horizontal");
    20.         float moveVertical = Input.GetAxis ("Vertical");
    21.  
    22.         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
    23.  
    24.         rb.AddForce (movement);
    25.     }
    26. }
    27.  
    But when I run it I'm getting this error:
    ArgumentException: Input Axis horizontal is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:19)


    I went to edit > project settings > input, and It looks like everything is set?
     
  6. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Try "Horizontal" in place of "horizontal"
     
    Timelog and Kiwasi like this.