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

C# Error Code in Unity 5

Discussion in 'Scripting' started by sonja_crocker, Mar 8, 2015.

  1. sonja_crocker

    sonja_crocker

    Joined:
    Mar 8, 2015
    Posts:
    5
    Hello everyone!

    I am new to Unity and the only coding I have ever done is HTML.:)

    I have been following the tutorial at http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player

    I am at the point where I have to write scripting in C#. Here is what he told me to write in the code:

    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}


    And here is the error code I get:


    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)'

    Since I have no prior C# knowledge, I am having trouble understanding this. I have looked through several knowledge base type articles and they only confuse me more. Any help would be greatly appreciated.

    Thanks in advance!
     
  2. ptr0x

    ptr0x

    Joined:
    Dec 9, 2013
    Posts:
    54
    Try to use rigidbody.AddForce instead of Rigidbody.AddForce.
     
  3. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    First, please use Code tags.

    Second, use ridigbody instead of RigidBody
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Rigidbody is a class; you need to refer to the specific instance, by using GetComponent<Rigidbody>().

    But don't use rigidbody since the component shortcuts are removed in Unity 5 and won't work. Even if you're using Unity 4 it's a good idea to stop using them.

    --Eric
     
    ptr0x likes this.
  5. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    That's a thing? Gah, that sucks. Guess I need to start making variables for them then... I'm so used to using the ones built in.
     
  6. sonja_crocker

    sonja_crocker

    Joined:
    Mar 8, 2015
    Posts:
    5
    @RiokuTheSlayer
    What do you mean use Code tags? I have never posted to a forum before and I have never used code either. I am REALLY new. I thought that Unity would be more like Blender. I think I have really gotten in way over my head. :confused:
    I am using Unity 5. So what should I use instead of rigidbody?
     
  7. sonja_crocker

    sonja_crocker

    Joined:
    Mar 8, 2015
    Posts:
    5
    Does anyone know of a good beginner's tutorial for Unity 5? All the ones I keep finding are for 4 or 4.1. That would help me out so much.
     
  8. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Check this page

    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
    sonja_crocker likes this.
  9. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Unity 5 JUST came out. Most of the tutorials for Unity 4 still appl, but with minor changes to the code
     
  10. sonja_crocker

    sonja_crocker

    Joined:
    Mar 8, 2015
    Posts:
    5
    Thanks for that! I just read it. So here is the right code.

    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. }
    Like that?
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Line 13 should be:
    Code (CSharp):
    1. GetComponent<Rigidbody>().AddForce (movement);
    Although you could also do:
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     private Rigidbody rigidbody;
    4.     void Awake()
    5.     {
    6.         rigidbody = GetComponent<Rigidbody>();
    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.  
    16.         rigidbody.AddForce (movement);
    17.     }
    18. }
     
    sonja_crocker likes this.
  12. sonja_crocker

    sonja_crocker

    Joined:
    Mar 8, 2015
    Posts:
    5
    Thank you so much guys! I am downloading version 4 now. The tutorial I am using is for that version. I figured it would probably help a whole lot. It might even confuse me less.
     
  13. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    I was going to suggest that. Use 4 for now. Later, once you know more, upgrade