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

Problems with Unity5 C# scripting (just starting out)

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

  1. Upsidedownmon

    Upsidedownmon

    Joined:
    Mar 16, 2015
    Posts:
    3
    Hey there Unity Community,

    I'm am a total beginner with scripting in Unity, I just started a few days ago. And I think I'm having problems with the new Unity5 C# scripting changes. At the moment I'm trying to make an object move like in the first tutorial, but I can't seem to get it to work.
    This is my code:

    using UnityEngine;
    using System.Collections;

    Public class RainboController : MonoBehaviour
    {
    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVerticle = Input.GetAxis ("Verticle");

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

    Rigidbody.AddForce (movement);
    }
    }

    I then ran a debug and this is what it said about the line:
    Rigidbody.AddForce (movement);

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

    I'm not sure how I need to "reference the object".
    The object's name is "Rainbo" if that helps.
    Also the object has a rigidbody component and the script RainboController is a component of the object.

    I realize there could be many things going on here (that just seems to be how programming is I guess), but any and all help I can get will be greatly appreciated.
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    You can't use "Rigidbody.AddForce" because Unity has no idea which rigidbody you're referring to. If you're trying to access the rigidbody attached to the same GameObject as that script is on, use:

    Code (CSharp):
    1. Rigidbody rigidBody = GetComponent<Rigidbody>();
    To create a reference to it (called rigidBody), then use rigidBody.AddForce();

    Generally, you should do this GetComponent call in a Start() method rather than in the FixedUpdate to cache it in the variable. Then use the variable in FixedUpdate.
     
    Upsidedownmon likes this.
  3. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    556
    You need to reference the current object... this keyword should word.
    Try using this.getcomponent<rigid body>().add force
    I'm typing on a tablet... So use your imagination here.


    And please...next time use the code tags to show your code....makes it a lot easier to read
     
    Upsidedownmon likes this.
  4. Upsidedownmon

    Upsidedownmon

    Joined:
    Mar 16, 2015
    Posts:
    3
    Jason Bricco,
    I tryed what you showed me but I still don't get what I'm doing wrong, like I tryed to put that in in a bunch of different places, but nothing was working for me. I still don't know what I'm doing as far as scripting goes. Is Unity planning on making any new tutorials for the unity 5 scripting changes? Is there anyone out that that me be able to help me out? Just to be as completely clear as I can be, I have never scripted before now. And I'm trying to figure it out, but then unity 5 came out and all the tutorials I'm finding on unity3d.com and youtube and on the net seem like they are all out of date. I'm not sure what I can do?
     
  5. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    Try

    transform.Rigidbody.AddForce (movement);

    This is what Jason Bricco is saying

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. Public class RainboController : MonoBehaviour
    5. {
    6. // Variable of type rigidbody
    7. RigidBody rigidBody;
    8. void Start()
    9. {
    10. //get a reference to the rigidbody attached to the transform
    11. rigidBody = GetComponenet<RigidBody>()
    12. }
    13. void FixedUpdate ()
    14. {
    15. float moveHorizontal = Input.GetAxis ("Horizontal");
    16. float moveVerticle = Input.GetAxis ("Verticle");
    17.  
    18. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVerticle);
    19.  
    20. //We created a reference to the rigidBody attached to the transform in the Start function which we can apply the force to
    21. rigidBody.AddForce (movement);
    22. }
    23. }
    24.  
    If you have never coded before then I would suggest you learn how to program instead of diving straight into games programming.
     
    Upsidedownmon likes this.
  6. Upsidedownmon

    Upsidedownmon

    Joined:
    Mar 16, 2015
    Posts:
    3
    yea I gonna try to find some more tutorials for C#
     
  7. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    The code is case sensitive so there is a difference between RigidBody and rigidBody, and there if a difference between Gameobject, and gameobject, Transform and transform etc.
    In Unity the convention is to use the upper case for the class, and lowercase for the instance of the class.


    If you really are just starting out there are many tutorials (and books) on C# around that will teach the general syntax of the language.

    [edit: i was too slow, see you already realized this last part]
     
    Upsidedownmon likes this.