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

Desperate need of help, Searched entire web....

Discussion in 'Getting Started' started by mcarthur03, Sep 3, 2015.

  1. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    I am new to unity, I have watched many tutorials, made sure they are the same version of unity, I copy their scripts EXACTLY 100% and yet whenever I try simple code like

    float input = Input.GetAxis ("Vertical");
    Rigidbody2D.AddForce (gameObject.transform.up * speed * input);

    I get a error saying "an object reference is required to access non-static member UnityEngine.Rigidbody2D.AddForce"
    I can solve this by making a public variable for the rigidbody and then using that variable instead of rigidbody directly. but in the videos no one has to do this. Please help!!!!
     
  2. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    heres the entire script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerShip : MonoBehaviour {
    5.  
    6.     public float speed;
    7.  
    8.     void FixedUpdate()
    9.     {
    10.         var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    11.         Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
    12.  
    13.         transform.rotation = rot;
    14.         transform.eulerAngles = new Vector3 (0, 0, Transform.eulerAngles.z);
    15.         Rigidbody2D.angularVelocity = 0;
    16.  
    17.         float input = Input.GetAxis ("Vertical");
    18.         Rigidbody2D.AddForce (gameObject.transform.up * speed * input);
    19.  
    20.  
    21.     }
    22.  
    23.  
    24. }
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The shortcut accesor .rigidbody2D was deprecated from 5.0 onwards.

    Use GetComponent<Rigidbody2D>() instead
     
    Xenoun likes this.
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    in other words:
    Are you sure about this?

    Also, the word "rigidbody2D" in that code would be lower case (uppercase refers to the abstract class, lowercase refers to the component on the object) so either this isn't true:
    or the code in that tutorial is incorrect.
     
    Kiwasi and Schneider21 like this.
  5. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    I suppose this would work for getting things like animations as well?

    edit
    because I have a script that has this

    anim = GetComponent<Animator>();

    and it dosent work
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    What is it giving for an error message?
     
  7. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    Or more generally, what does "doesn't work" mean? That's a pretty vague statement; was there a code error? (in which case, what he asked) Otherwise, what happens?
     
  8. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    There is no error, it seems to simply skip that line and all others about anim....
     
  9. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    um why do you think that? The computer doesn't just skip lines of code (unless of course that entire function isn't being run; put in Debug.Log messages to see). What makes you think that is happening? What are you seeing when you hit Play?
     
  10. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    So first i make a Animator named anim

    Animator anim;

    Then in void start:

    anim = GetComponent<Animator>();

    then in void FixedUpdate

    anim.SetFloat("Speed", Mathf.Abs(move));

    In my animator speed is a float, move is a float in the code which changes between 1 and 0 when i give it input, the move float works.

    in my animator Speed is used in a transition in which when greater then zero it switches from the default to a walking animation, when Speed is zero or less it swaps back.

    I get no errors, nothing. the animation simply doesn't swap when i move the player...
     
  11. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    Okay, there are lots of possible reasons why the animation isn't playing. You shouldn't just jump to assuming the computer is arbitrarily skipping lines of code :p

    One of the most common mistakes setting up animation transitions is to only go one way and forget to transition back, so check that. Also, can you post a screenshot of the animator window?

    (tangent: if you're having trouble with the tutorials you've found, maybe you should checkout Unity in Action)
     
  12. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    I fixed it just before you replied,i simply had to replace

    anim.SetFloat("Speed", Mathf.Abs(move));

    With

    anim.SetFloat("Speed", move);