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

My CSharp Script Isn't Working

Discussion in 'Scripting' started by Tartoken, Aug 15, 2015.

  1. Tartoken

    Tartoken

    Joined:
    Aug 15, 2015
    Posts:
    6
    I am using this script in a project of mine.
    I am an absolute beginner.
    Here is the script:
    using UnityEngine;
    using System.Collections;

    public class PlayerMovement : MonoBehaviour {
    public float moveSpeed;

    private Vector3 input;


    // Use this for initialization
    void Start () {

    }

    void Update () {
    input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    Rigidbody.AddForce(input * moveSpeed);
    }
    }

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

    Please help me out, thanks.
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    You'll need to use GetComponent :
    Code (CSharp):
    1. GetComponent<Rigidbody>().AddForce(input * moveSpeed);
     
    lordconstant likes this.
  3. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Heyho,
    Please add code tags to you code. The code will be much more readable and it will add line numbers to it.
    The Rigidbody in your code is the rigidbody class itself. You need to use an instance of the Rigidbody class to do something with it.
    View the class as the template and the instance as the actual object that is made from the template. (See this tutorial for more about classes: http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes?playlist=17117. It's an important concept with C#!)
    If you gameobject does contain a rigidbody component (you'll have to add it via the inspector), then the rigidbody component of the gameobject is the actual instance of the Rigidbody class. You just have to get a reference to that component. That's what the GetComponent (http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html) function is for.
    So you need to do:
    Code (CSharp):
    1. GetComponent<Rigidbody>().AddForce(input * moveSpeed);
     
  4. Tartoken

    Tartoken

    Joined:
    Aug 15, 2015
    Posts:
    6
    Ok I have added the new code in, but now I have encountered a new error about the CSharpAssembly.dll file, something about it missing.
    I haven't found a single forum thread on this issue so if anyone can Identify where the file is located or what it is that would be greatly appreciated.
     
  5. Tartoken

    Tartoken

    Joined:
    Aug 15, 2015
    Posts:
    6
    Ok, here is the exact error:
    Moving Temp/Assembly-CSharp.dll to Library/ScriptAssemblies/Assembly-CSharp.dll:The system cannot find the file described.
    Any help would be greatly appreciated
     
  6. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hey Tartoken,

    Could you please show you actual code? Thanks!
     
  7. Tartoken

    Tartoken

    Joined:
    Aug 15, 2015
    Posts:
    6
    I doubt it has anything to do with the code as my original error had disappeared when I had added everything that you and others had said to add, but here it is anyway:
    using UnityEngine;
    using System.Collections;

    public class PlayerMovement : MonoBehaviour {
    public float moveSpeed;

    private Vector3 input;


    // Use this for initialization
    void Start () {

    }

    void Update () {
    input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    GetComponent<Rigidbody>().AddForce(input * moveSpeed);
    }
    }

    P.S. I still don't know what you mean by code tags or how to add them in.
     
  8. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    There's a icon bar on top of the editor. The 6th icon from right provides an option to enclose code in code tags, which shows you code like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5. public float moveSpeed;
    6.  
    7. private Vector3 input;
    8.  
    9.  
    10. // Use this for initialization
    11. void Start () {
    12.  
    13. }
    14.  
    15. void Update () {
    16.  input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    17.  GetComponent<Rigidbody>().AddForce(input * moveSpeed);
    18. }
    19. }
    The code looks ok to me. Probably a Unity problem?
     
  9. Tartoken

    Tartoken

    Joined:
    Aug 15, 2015
    Posts:
    6
    Ah ok, thanks for telling me how to do this. I will probably pass this problem onto Unity support as I have found no solution to this. Thank you for helping anyway.
     
  10. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    There are some answers regarding to this error. Like
    Are you using any cloud stuff or syncing?
     
  11. Tartoken

    Tartoken

    Joined:
    Aug 15, 2015
    Posts:
    6
    Nope, I haven't used any cloud saving or syncing for files, All I have done is install Unity so far.