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

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!!!

heres the entire script

using UnityEngine;
using System.Collections;

public class PlayerShip : MonoBehaviour {

    public float speed;

    void FixedUpdate()
    {
        var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);

        transform.rotation = rot;
        transform.eulerAngles = new Vector3 (0, 0, Transform.eulerAngles.z);
        Rigidbody2D.angularVelocity = 0;

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


    }


}

The shortcut accesor .rigidbody2D was deprecated from 5.0 onwards.

Use GetComponent() instead

1 Like

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.

2 Likes

I suppose this would work for getting things like animations as well?

edit
because I have a script that has this

anim = GetComponent();

and it dosent work

What is it giving for an error message?

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?

There is no error, it seems to simply skip that line and all others about anim…

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?

So first i make a Animator named anim

Animator anim;

Then in void start:

anim = GetComponent();

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…

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 :stuck_out_tongue:

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)

I fixed it just before you replied,i simply had to replace

anim.SetFloat(“Speed”, Mathf.Abs(move));

With

anim.SetFloat(“Speed”, move);