-2

I was following a tutorial on how to make a platformer (I'm new) and couldn't figure out why it gives me Null Reference Exception whenever I run the project, any help would be appreciated.

thank you!

The code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movement : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;

    void start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        moveInput = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    }
}

full error message: NullRefrenceException: Object reference not set to an instance of an object movement.FixedUpdate () (at Assets/movement.cs:21)

cur
  • 17
  • 1
  • `Input`, `rb` or `rb.velocity` is null. Please add debugging info. Could narrow the list by knowing which is line 21. – phuzi May 05 '22 at 12:25
  • 1
    If `FixedUpdate` is called before `start` then `rb` will be null and give you this exception. I don't know much about Unity, but perhaps the `start` method should actually be called `Start`? – DavidG May 05 '22 at 12:27
  • I second the start => Start suggestion, but anyway the information in the duplicate should make you able to find the problem, which probably is that rb is null. – Palle Due May 05 '22 at 12:32

0 Answers0