-6

I have a NullRefeneceExpection and I do not know why, it is about a Text Object called SetCountText(). I would appriciate if someone could help me out. Error: NullReferenceExpection: Object reference not set to an instance of an Object PlayerController.SetCountText() (at Assets/Scripts/PlayerController.cs:48)

Here is the PlayerControler Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using TMPro;

public class PlayerController : MonoBehaviour
{
    public float speed = 0;
    public Text countText;
    public GameObject winTextObject;
    public Text liveTextObject;
    public GameObject loseTextObject;
    public GameObject Enemy;

    private Rigidbody rb;
    private int count;
    private int live;
    private float movementX;
    private float movementY;

    // Start is called before the first frame update
    void Start()
    {
        live = 5;
        rb = GetComponent<Rigidbody>();
        count = 0;

        SetCountText();
        winTextObject.SetActive(false);

        SetLiveText();
        loseTextObject.SetActive(false);
    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x;
        movementY = movementVector.y;
    }

    void SetCountText()
    {
        countText.text = "Count:" + count.ToString();
        if (count >= 12)
        {
            winTextObject.SetActive(true);
        }
    }

    void SetLiveText()
    {
        liveTextObject.text = "Live:" + live.ToString();
        if(live <= 1)
        {
            loseTextObject.SetActive(true);
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement * speed);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("PickUp"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;

            SetCountText();
        }

        if (other.gameObject.CompareTag("Enemy"))
        {
            other.gameObject.SetActive(false);
            live = live - 1;
        }
    }

    void resetPlayer()
    {
        live -= 1;
        liveTextObject.text = "lives" + live;

        /*if (lives == 0)
        {

        }*/
    }
}
  • You do not appear to ever initialise `countText` or `winTextObject`, in which case they'll both be `null` and create a `NullReferenceException` when they are dereferenced. You need to assign them both a value somewhere. – Martin Costello Jun 01 '22 at 10:19
  • You are making use both of reference and value types inside your `PlayerController`. Value type like `int` and `float` cannot cause a `NullReferenceException` but reference types do. You must assign/instantiate them in order to use them. You do instantiate `Rigidbody rb` in your `Start()` event (that is good), but other reference types like `Text` and `GameObject` should be instantiated before you access their properties or call their methods. – Cleptus Jun 01 '22 at 10:22
  • I am not a Unity guy, but `Text countText` initially is `null` (because of lack of initialization), the first event, `Start()` is executed and . `Start() -> SetCountText() -> countText.text`. If that line executes and `countText == nul` then you get the exception. – Cleptus Jun 01 '22 at 10:26

0 Answers0