Coding Help (580264)

My code
using UnityEngine;
using System.Collections;

public class New : MonoBehaviour {

public float speed = 6.0f;

void FixedUpdate(){
{
//apply forward movement
gameObject.GetComponent().velocity = new Vector2(speed, Rigidbody2D.velocity.y);
}
}
}

My problems
Assets/CharacterMove.cs(11,106): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity’

Assets/CharacterMove.cs(11,117): error CS1502: The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)’ has some invalid arguments

Assets/CharacterMove.cs(11,117): error CS1503: Argument #2' cannot convert object’ expression to type `float’

Assets/CharacterMove.cs(11,49): error CS0246: The type or namespace name `RigidBody2D’ could not be found. Are you missing a using directive or an assembly reference?

I need help. I’m doing this for a school project and I thought it would be fun to make a game but it turns out to be very overwhelming. I don’t understand what I did wrong because I watched it off a tutorial. And don’t worry i’m not procrastinating on this project, its not due tomorrow

Change “RigidBody2D” to “Rigidbody2D” in your GetComponent<> statement.
You will also probably need to change “Rigidbody2D.velocity.y” to “GetComponent().velocity.y”.

In all, it should look like

gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);

Try that and report back.

Thank you so much. When it started working, i just jumped for joy

I don’t think the video tutorial I am using works. I keep typing in the code just like he does but it doesn’t seem to work
Here is the link:

Here is the code I made from it at around 14 minutes
using UnityEngine;
using System.Collections;

public class CameraBehavior : MonoBehaviour {

Transform player;
public float horizontaloffset = 6f;

// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag (“Player”).tranform;
}

// Update is called once per frame
void Update () {
var x = player.position.x;

transform.position = new Vector3 (x + horizontaloffset, transform.position.y, -10);
}
}
And here is the error it gave me
Assets/CameraBehavior.cs(11,70): error CS1061: Type UnityEngine.GameObject' does not contain a definition for tranform’ and no extension method tranform' of type UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)

PLEASE, always use code tags when posting source code.

Also, you wrote “tranform” instead of “transform.” You should get familiar with code completion.

im sorry, how do i put a code tag?

[ code]Put your code here.[ /code]
You will need to remove the spaces for it to show up. Here’s what it looks like without the spaces inside the code brackets.

Put your code here.
sing UnityEngine;
using System.Collections;

public class CharacterMovement : MonoBehaviour {

    public float speed = 6.0f;
    Transform groundcheck;
    private float overlapRadius = 0.2f;
    public LayerMask whatIsGround;
    private bool grounded = false;
    private bool jump = false;
    public float jumpForce = 600f;
    private bool doubleJump = false;
    Animator anim;

    void Start()
    {
        groundcheck = transform.Find ("groundcheck");
        anim = GetComponent<Animator> ();
    }

    void Update()
    {
        if (Input.GetKeyDown (KeyCode.Space))
            jump = true;
    }

    void FixedUpdate()
    { 
        //Check if character is grounded
        grounded = Physics2D.OverlapCircle (groundcheck.position, overlapRadius, whatIsGround);

        //Reseting doubleJump whe we are on the ground
        if (grounded)
            doubleJump = false;

        //Determine if player can jump
        bool canJump = (grounded || !doubleJump);

        //Set animation
        anim.SetBool ("jumping", canJump && jump);

        if (jump && canJump) 
        {
            Rigidbody2D.velocity = new Vector2(Rigidbody2D.velocity.x, 0);
            Rigidbody2D.AddForce(new Vector2(0, jumpforce));

            if (!grounded)
                doubleJump = true;
        }

        jump = false;

        //apply forward movement
        gameObject.GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
    }
}

Errors:
Assets/CharacterMovement.cs(45,72): error CS0120: An object reference is required to access non-static member UnityEngine.Rigidbody2D.velocity' Assets/CharacterMovement.cs(45,86): error CS1502: The best overloaded method match for UnityEngine.Vector2.Vector2(float, float)’ has some invalid arguments
Assets/CharacterMovement.cs(45,86): error CS1503: Argument #1' cannot convert object’ expression to type float' Assets/CharacterMovement.cs(45,37): error CS0120: An object reference is required to access non-static member UnityEngine.Rigidbody2D.velocity’
Assets/CharacterMovement.cs(46,37): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.ForceMode2D)’

I am stumped on this, i did what the tutorial said to do and I’m just not sure what happened. Anywho, I hope i put the code tag right

When you get the error An object reference is required to access non-static member, this means you need an instance of the object to use this member. Rigidbody2D is a class, not an object of this class.

Add a variable private Rigidbody2D m_Rigidbody; then in your Start() method, set this to the attached Rigidbody2D like this m_Rigidbody = GetComponent<Rigidbody2D>(); and finally replace Rigidbody2D.velocity and Rigidbody2D.AddForce with m_Rigidbody.velocity and m_Rigidbody.AddForce.

You can now also replace gameObject.GetComponent().velocity with m_Rigidbody.velocity. It’s good practice to store this in a variable so you don’t have to call GetComponent all the time.