Question by
Graapus09 · Oct 24, 2016 at 04:45 PM ·
error messagenullreferenceexception
How do i fix this nullreferenceException
Hi, i am very new to unity and i just tried to make a block dodge game posted to youtube (Brackeys https://www.youtube.com/watch?v=tyAutnOlsfA) evrything went good until i begun coding, i believed i filled out the exact same as he did, but i got an error saying: NullReferenceException: object not set to an intance of an object Player.fixedupdate () (atassets/player.cs:19) line 19 is the
rb.MovePosition(rb.position + Vector2.right * x);
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float speed = 15f;
private Rigidbody2D rb;
void start ()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate ()
{
float x = Input.GetAxis("Horizontal") * Time.fixedDeltaTime * speed;
rb.MovePosition(rb.position + Vector2.right * x);
}
}
Comment
Best Answer
Answer by Denvery · Oct 24, 2016 at 05:23 PM
This error is probably due to rb == null. Please be sure that the game object with your script has Rigidbody2D component.
Also - write "Start" with capital "S". It's mistake to write it with lowercase letter.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095335im_/https://answers.unity.com/themes/thub/images/avi.jpg)