I’m just getting starting with the basics of Unity and was trying to get a timer to countdown from 15 seconds for the player to collect all the objects, but I’m receiving an error saying “NullReferenceException: Object reference not set to an instance of an object”. Anyone know what I’m doing wrong?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text timeText;
private Rigidbody rb;
private int count;
private float timeLeft;
void Start() {
rb = GetComponent<Rigidbody> ();
count = 0;
timeLeft = 15.0f;
setCountText ();
Cursor.visible = false;
}
void Update() {
timeLeft -= Time.deltaTime;
timeText.text = "Time left: " + Mathf.Round (timeLeft);
if (timeLeft < 0f) {
SceneManager.LoadScene ("GameOver");
}
}
void FixedUpdate() {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("Pick Up")) {
other.gameObject.SetActive (false);
count = count + 1;
setCountText ();
}
}
void setCountText() {
countText.text = "Count: " + count.ToString ();
if (count >= 8) {
SceneManager.LoadScene ("End Screen");
Cursor.visible = true;
}
}
}