# Coding Help (580264)

**URL:** https://discussions.unity.com/t/coding-help-580264/580264
**Category:** 2D
**Tags:** 2D
**Created:** [May 12, 2015, 8:45am UTC](https://discussions.unity.com/t/coding-help-580264/580264 "2015-05-12T08:45:17Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Khanh808](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Khanh808](https://discussions.unity.com/u/Khanh808)
#### Post date: [May 12, 2015, 8:45am UTC](https://discussions.unity.com/t/coding-help-580264/580264/1 "2015-05-12T08:45:17Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Khanh808](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Khanh808](https://discussions.unity.com/u/Khanh808)
#### Post date: [May 12, 2015, 8:47am UTC](https://discussions.unity.com/t/coding-help-580264/580264/2 "2015-05-12T08:47:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Aaron\_T](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aaron_t/32/389856_2.png) [@Aaron\_T](https://discussions.unity.com/u/Aaron_T)
#### Post date: [May 12, 2015, 10:01am UTC](https://discussions.unity.com/t/coding-help-580264/580264/3 "2015-05-12T10:01:42Z")

</div>

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

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

```

Try that and report back.

---

<div class="post-metadata">

### Author: ![Khanh808](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Khanh808](https://discussions.unity.com/u/Khanh808)
#### Post date: [May 13, 2015, 12:30pm UTC](https://discussions.unity.com/t/coding-help-580264/580264/4 "2015-05-13T12:30:06Z")

</div>

> [@Aaron\_T](#):
>
> 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
> 
> ```auto
> 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

---

<div class="post-metadata">

### Author: ![Khanh808](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Khanh808](https://discussions.unity.com/u/Khanh808)
#### Post date: [May 13, 2015, 12:41pm UTC](https://discussions.unity.com/t/coding-help-580264/580264/5 "2015-05-13T12:41:29Z")

</div>

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:

[![](https://europe1.discourse-cdn.com/unity/original/4X/6/5/8/658a948b3eec773ec3c25e695832c7778ccfadeb.jpeg "Basics and Mechanics of a 2D Endless Runner w/ Unity - Part I") ](https://www.youtube.com/watch?v=KIBQqCSGXmM)

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?)

---

<div class="post-metadata">

### Author: ![blizzy](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/blizzy/32/488177_2.png) [@blizzy](https://discussions.unity.com/u/blizzy)
#### Post date: [May 13, 2015, 2:29pm UTC](https://discussions.unity.com/t/coding-help-580264/580264/6 "2015-05-13T14:29:14Z")

</div>

**PLEASE** , always use code tags when posting source code.

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

---

<div class="post-metadata">

### Author: ![Khanh808](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Khanh808](https://discussions.unity.com/u/Khanh808)
#### Post date: [May 13, 2015, 6:06pm UTC](https://discussions.unity.com/t/coding-help-580264/580264/7 "2015-05-13T18:06:04Z")

</div>

im sorry, how do i put a code tag?

---

<div class="post-metadata">

### Author: ![Aaron\_T](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aaron_t/32/389856_2.png) [@Aaron\_T](https://discussions.unity.com/u/Aaron_T)
#### Post date: [May 13, 2015, 7:11pm UTC](https://discussions.unity.com/t/coding-help-580264/580264/8 "2015-05-13T19:11:27Z")

</div>

> [@Khanh808](#):
>
> 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.

```auto
Put your code here.

```

---

<div class="post-metadata">

### Author: ![Khanh808](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Khanh808](https://discussions.unity.com/u/Khanh808)
#### Post date: [May 15, 2015, 9:13am UTC](https://discussions.unity.com/t/coding-help-580264/580264/9 "2015-05-15T09:13:15Z")

</div>

```auto
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

---

<div class="post-metadata">

### Author: ![steego](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/steego/32/924633_2.png) [@steego](https://discussions.unity.com/u/steego)
#### Post date: [May 15, 2015, 4:24pm UTC](https://discussions.unity.com/t/coding-help-580264/580264/10 "2015-05-15T16:24:36Z")

</div>

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](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=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.
