Home > OS >  Object reference not set to an instance of object. Unity
Object reference not set to an instance of object. Unity

Time:07-16

I am really new to unity and i'm trying to make a PlayerController with different states in different files.

this is the code so far:

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

public class PlayerController : MonoBehaviour
{
private PlayerState currentState;

void Start()
{
    currentState = new PlayerIdle(this);
    Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
    currentState.OnStateUpdate();
}
public void ChangeState(PlayerState newState)
{
    currentState.OnStateExit();
    currentState = newState;
    newState.OnStateEnter();
}
}

how do i fix this?

edit: here is the PlayerIdle code which is supposed to give me the error.

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

public class PlayerIdle : PlayerState
{
public CharacterController controller;
public Transform cam;
public GameObject player = GameObject.Find("Player");

public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3;
public float rotationSpeed;

float x;
float z;

public Transform groundCheck = GameObject.Find("groundCheck").transform;
public float groundDistance = 0.4f;
public LayerMask groundMask;

[SerializeField] Vector3 velocity;
public bool isGrounded;

public float turnSmoothTime = 1f;
float turnSmoothVelocity;

public PlayerIdle(PlayerController playerController) : base(playerController)
{
    this.playerController = playerController;
}

public override void OnStateEnter()
{

}
public override void OnStateExit()
{

}


public override void OnStateUpdate()
{
    x = Input.GetAxisRaw("Horizontal");
    z = Input.GetAxis("Vertical");
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    velocity = player.transform.TransformDirection(Vector3.forward) * speed * z   new Vector3(0f, velocity.y, 0f); ;

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    if (isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }


    velocity.y  = gravity * Time.deltaTime;

    player.transform.Rotate(new Vector3(0f, x * rotationSpeed, 0f));
    controller.Move(velocity * Time.deltaTime);
}

}

any help would be much appreciated :)

also, i found out this is called a statemachine if that helps.

CodePudding user response:

The error says that something in that line is null. The only thing that can be null is the CurrentState variable. Probably when you assign it the value of new PlayerIdle (this) in the Start () method, that value is null. If you can figure out why it's null, you can fix it, but if you're still having trouble you can include the PlayerIdle () code. A suggestion may be to insert a print (currentState) at the end of Start () to understand if it is actually null. In addition, also insert a print (new PlayerIdel (this)) in the Update () method to understand if even during the update it continues to be null.

CodePudding user response:

it seems that the update method was being called before the start method (which i still don't understand), so the solution was just to add an if statement before currentState.OnStateUpdate(); saying "if (currentState != null)"

  • Related