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)
{
}*/
}
}