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

Error in Space Shooter Project Script

Discussion in 'Scripting' started by Dev_23, Oct 4, 2014.

  1. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18
    This is the code the tutorial says to write:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.    
    6.     void FixedUpdated () {
    7.         float moveHorizontal = Input.GetAxis ("Horizontal");
    8.         float moveVertical = Input.GetAxis ("Vertical");
    9.        
    10.         Vector3 movement = new Vector3 ( moveHorizontal, 0.0f, moveVertical);
    11.         Rigidbody.velocity = movement;
    12.     }
    13. }
    And I get the error:
    Assets/Scripts/PlayerController.cs(11,39): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.velocity'


    After changing the Rigidbody.velocity to UnityEngine.Rigidbody.velocity and resaving and loking at the console, the same error is there. Any way to get it to work properly?

    Thanks.
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    The word rigidbody on line 11 should be lowercase.
    Code (CSharp):
    1. rigidbody.velocity = movement;
    Also, I'm sure this was just a typo, but the method should be FixedUpdate, not FixedUpdated.
     
    Dev_23 likes this.
  3. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18
    Thank you mayor!
     
  4. Kadra

    Kadra

    Joined:
    Dec 9, 2014
    Posts:
    5
    Thanks so much, i was stuck on the exact same thing

    Thanks for making this thread Dev_23 and thanks mayor aswell
     
  5. invictus011

    invictus011

    Joined:
    Apr 10, 2015
    Posts:
    1
    Please help, I'm new in Unity. I have error with Space Shooter tutorial.

    My code is :

    using UnityEngine; using System.Collections;

    public class PlayerController : MonoBehaviour
    {

    void FixedUpdate()
    {
    1. float moveHorizontal=Input.GetAxis("Horizontal");
    2. float moveVertical=Input.GetAxis("Vertical");
    3. Vector3 movement =newVector3(moveHorizontal,0.0f, moveVertical);
    4. rigidbody.velocity = movement;
    5. }
    }

    and I have error massage

    Assets/Scripts/PlayerController.cs(13,27): error CS1061: Type UnityEngine.Component' does not contain a definition for velocity' and no extension method velocity' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    DowntownBrownGames likes this.
  7. Jami_2

    Jami_2

    Joined:
    Apr 22, 2013
    Posts:
    19
    If you working below Unity5 you can access rigidbody with script attached to.

    Code (CSharp):
    1. rigidbody.velocity
    In unity5 you get with GetCompent, but be careful do it in Start () to get better Performance.

    Code (CSharp):
    1. private Rigidbody rb;
    2.  
    3. void Start ()
    4. {
    5.         rb = GetCompent<Rigidbody>();
    6. }
    7.  
    8. void Update ()
    9. {
    10.       rb.velocity = Doanything();
    11.       //or stop it
    12.       rb.velocity = Stopanything();
    13. }
    Write it on my phone can have typos;)
     
  8. zaheeriub

    zaheeriub

    Joined:
    Jul 31, 2015
    Posts:
    5
    My script is running but there is no any movement in it
    Please help what is the Problem with it
     
  9. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    then you should post your code that we can see what you are doing
     
  10. zaheeriub

    zaheeriub

    Joined:
    Jul 31, 2015
    Posts:
    5
  11. zaheeriub

    zaheeriub

    Joined:
    Jul 31, 2015
    Posts:
    5
    That is C# code there is no any error but my object is not moving
     
  12. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    rfnftdybrjkfq likes this.
  13. zaheeriub

    zaheeriub

    Joined:
    Jul 31, 2015
    Posts:
    5
    upload_2015-7-31_17-56-38.png
     
    richeyt likes this.
  14. zaheeriub

    zaheeriub

    Joined:
    Jul 31, 2015
    Posts:
    5
    i also do that
     
  15. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    don't do a GetComponent in the FixedUpdate, move line 12 to
    void Start()
    {
    LINE 12 HERE
    }
     
  16. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Can you make a debug log

    after Vector3 movement = .....
    Code (CSharp):
    1. Debug.Log("HorizontalValue: " + movehorizontal + " VerticalValue " + movevertcal + " new Vector: " + movement);
    to see if you even get input
     
  17. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    1. FixedUpdate() needs to be capitalized correctly- it's not running at all, like it is.
    2. Move your Rigidbody reference to be a class member instead of assigning it every update, which martinmr already mentioned- assign it in Awake or Start and then just use the reference from then on
    3. Don't assign a velocity directly, use AddForce() instead.
     
  18. Connor36

    Connor36

    Joined:
    Jul 2, 2017
    Posts:
    3
    I know this thread is a little old but it is the only one talking about this problem. I have read through it but even when there were no errors my ship would not move. Here is my code. Thanks in advance.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour {
    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement;

    }
    void Start()
    {
    Rigidbody rigidbody = GetComponent<Rigidbody> ();
    }

    }
     
  19. tmwolf100

    tmwolf100

    Joined:
    Jul 10, 2017
    Posts:
    1
    Connor36, I think you missed the line:

    private Rigidbody rigidbody;

     
    eyearling likes this.
  20. CopyCat00997

    CopyCat00997

    Joined:
    Aug 20, 2017
    Posts:
    1
    uummm guys? I'm sorry but this thing is not working for me please tell me what can i do....

    using System.Collections;
    using UnityEngine;

    public class PlayerController : MonoBehaviour {

    void start()
    {
    Rigidbody rigidbody = GetComponent<Rigidbody> ();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement;
    }
    }
     
  21. karthikmanivel

    karthikmanivel

    Joined:
    Jun 27, 2017
    Posts:
    1
    CopyCat00997 You forgot to add private Rigidbody rb;

    This might work
    --------------------------------------------------------------------------------
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class PlayerController : MonoBehaviour
    {
    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody> ();

    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rb.velocity = movement;
    }
    }
    --------------------------------------------------------------------------------
     
    eyearling likes this.
  22. thom428

    thom428

    Joined:
    Nov 9, 2014
    Posts:
    1
    This does work. Question, does it matter if you use public or private before the start code? I tried both and both worked. Just curious if you should use one over the other, when and why.
     
  23. richeyt

    richeyt

    Joined:
    Jan 24, 2018
    Posts:
    1
  24. Deleted User

    Deleted User

    Guest

    This is the best answer I have seen and I have viewed this thread and tried the other solution and it didn't work, I suspect unity updates and naming of files and libraries etc... is why the other solutions also break (including a bizarre line endings being different for Unix or Mac or Windows) which is just the Visual Studio Compilers way of confusing itself into save. This also appeared as an error. Fixing the 1st error fixes both and they now work.

    So the code in the tutorial calls

    rigidbody
    cannot find it and generates the error. Your code then at the start of the function says


    GetComponent

    (ie something , a component within Unity)

    then says

    Rigidbody

    It seems after all that fuss, only the capitalisation of the function has changed (removed from one version into the next).

    Because


    Rigidybody

    is a class of Unity 5.1 it can find it. No errors in VS on save. Compile and run the program.

    It works.
     
  25. dreammaker9

    dreammaker9

    Joined:
    May 21, 2018
    Posts:
    3
    I'm doing this and I'm really new to unity. If somebody could help me with my script, I'd be really thankful

    using System.Collections;
    using UnityEngine;

    [System.Serializable]
    public class Boundary
    {
    public float xMin, xMax, zMin, zMax;
    }
    public class PlayerController : MonoBehaviour
    {
    public float speed;
    public float tilt;
    public Boundary boundary;

    private void Start()
    {
    Rigidbody rigidbody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement * speed;

    rigidbody.position = new Vector3
    (
    Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
    0.0F,
    Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
    );
    rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
    }

    everything I've underlined has had an error. If somebody could contact me with a fix, that'd be great.
     
  26. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    First, please check this thread for how to post code nicely on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Change this bit :
    Code (csharp):
    1. Rigidbody rb;
    2. private void Start()
    3. {
    4. rb = GetComponent<Rigidbody>();
    5. }
    Replace any 'rigidbody' with 'rb' in the rest of your code and report back how that goes. Please post with code tags, if you have more issues. In the future, it would be helpful if you included the error message when you post an issue, as well.

    Hope that helps. :)
     
  27. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think methos is correct. you're creating a temporary Rigid body in the Start(), once Start has completed. the rigidbody is no longer usable.
    So do as methos said and you should be good.
     
  28. unity_lCEWP6IO05-tcw

    unity_lCEWP6IO05-tcw

    Joined:
    Dec 19, 2018
    Posts:
    1
    Just to inform everyone, everyone is coming here because this is in one of the tutorial videos. The guy doing the video has what everyone has here which is correct however it is a discrepancy between the tutorial (which is for absolute beginners and possibly beginner coders ) and Unity 5..