Object refrence not set to an instance of an object

Hello! I have a problem on my code! Can anyone help? The error is “Object refrence not set to an instance of an object PlayerScript.HandleMovement() (Assets/Scripts/PlayerScript.cs59)”

Here is the code, I am using c#

1.using System.Collections;
2.using System.Collections.Generic;
3.using UnityEngine;
4.
5.public class PlayerScript : MonoBehaviour
6.{
7.[SerializeField] float sensitivityX = 1;
8.[SerializeField] float sensitivityY = -1;
9.
10.[SerializeField] float movementSpeed = 3;
11.
12. CharacterController characterController;
13.
14
15.[SerializeField] Transform cameraTransform;
16
17.float cameraRotationX = 0f;
18
19.//start is called before the first frame update
20.void start()
21.{
22.characterController = GetComponent();
23.
24.}
25.
26.// Update is called once per frame
27.void Update()
28.{
29HandleRotation();
30.HandleMovement();
31.}
32.
33.
34.void HandleRotation()
35.{
36.float inputX = Input.GetAxis(“Mouse X”) * sensitivityX;
37.float inputY = Input.GetAxis(“Mouse Y”) * sensitivityY;
38.
39.Vector3 rotation = new Vector3(0, inputX, 0);
40.
41.transform.Rotate(rotation);
42.
43.cameraRotationX -= inputY;
44.
45.cameraRotationX = Mathf.Clamp(cameraRotationX, -90, 90);
46.
47.cameraTransform.localRotation = Quaternion.Euler(cameraRotationX, 0, 0);
48.}
49.
50.
51.
52.void HandleMovement()
53.{
54.float inputX = Input.GetAxis(“Horizontal”);
55.float inputY = Input.GetAxis(“Vertical”);
56.
57.Vector3 movement = new Vector3(inputX, 0, inputY).normalized;
58.
59. characterController.Move(movement * Time.deltaTime * movementSpeed);
60.
61.}
62.}

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Be careful with capitalization: “start()” is not the same as “Start()”

3 Likes