-1

I can't figure out what the heck is wrong with this, the error is

NullReferenceException: Object reference not set to an instance of an object movment2D.FixedUpdate () (at Assets/movment2D.cs:21)

my code is

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

public class movment2D: MonoBehaviour
{
        public CharacterController2D controller;
 
    public float runSpeed = 40f;

    float horizontalMove = 0f;
 
    // Update is called once per frame
    void Update ()
    {
        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    }

    void FixedUpdate ()
    {
        controller.Move(horizontalMove * Time.fixedDeltaTime, false, false); <-- here
    }
}

it says the two false tags are the problem but idk why

1 Answers1

0

How are you assigning the reference to CarachterController2D character? in the inspector or by code? Make sure you initialize it with a value before FixedUpdate starts you can do this by code or in the inspector:

if its in the inspector drag and drop the CharacterController2D component into the controller property in the inspector of the movment2D script

if it is by code and its in the same game object initialize it before fixedUpdate like on start

private void Start()
{
 controller = GetComponent<CharacterController2D>();
}