I need help, this code is broken... RRS feed

  • Question

  • My code:using UnityEngine;

    public class Playerscript : MonoBehaviour
        public float MoveX = 1f;
        public float kiirus = 1f;
        public Rigidbody2D rb;
        public CharacterController2D controller;

        // Update is called once per frame
        void Update()
            MoveX = Input.GetAxisRaw("Horizontal") * kiirus;

        void FixedUpdate()
            controller.Move(MoveX * Time.fixedDeltaTime, false, false);
    My errors: When I try to launch the game, it starts spamming this:

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

    Tuesday, November 19, 2019 3:12 PM

All replies

  • Which line of code is the exception thrown ???

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Tuesday, November 19, 2019 3:33 PM
  • Given the error I'm wagering `controller` is null because you never initialize it. This however is related to Unity so I recommend you post in their forums if this field is supposed to be initialized by Unity somehow.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 20, 2019 2:58 PM