Hi, all.
New user of Unity here. I should go introduce myself sometime, but want to get to business right now.
I get a null reference exception error at two lines of code:
NullReferenceException: Object reference not set to an instance of an object
timer.Start () (at Assets/timer.cs:17)
-and-
NullReferenceException: Object reference not set to an instance of an object
timer.Update () (at Assets/timer.cs:27)
So here is the situation, along with the code:
I’m writing a very simple maze game with three pregenerated mazes, 3 by 3, 6 by 6, and 9 by 9.
I first created the 9 by 9 maze, put in start and end markers, a collision detector, and a timer, along with a canvas to display the time, in seconds, the player has been in the maze while navigating, and the final time in seconds when the player collides with the end marker. Once that worked, I created the two smaller mazes, and copied the canvas and scripts from the existing maze into the other two mazes.
3 by 3 and 9 by 9 work fine. 6 by 6 produces the error.
Here is my script, in its entirety:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class timer : MonoBehaviour
{
float myTimer;
int myTimeri;
bool timing;
// Start is called before the first frame update
void Start()
{
myTimer = 0.0f;
myTimeri = (int)myTimer;
timing = false;
GameObject.Find("timerUI").GetComponent<Text>().text = "";
}
// Update is called once per frame
void Update()
{
if (timing)
{
myTimer = myTimer + Time.deltaTime;
myTimeri = (int)myTimer;
GameObject.Find("timerUI").GetComponent<Text>().text = "" + myTimeri;
}
if (Input.GetKey("b"))
{
SceneManager.LoadScene("Load");
}
}
//detect collision with start or end time markers
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.collider.tag == "Start")
{
myTimer = 0.0f;
Destroy(hit.collider.gameObject);
timing = true;
}
if (hit.collider.tag == "End")
{
Destroy(hit.collider.gameObject);
timing = false;
GameObject.Find("timerUI").GetComponent<Text>().text = "The number of seconds you took to solve the maze was: " + myTimeri + ". Press the b key to return to the previous screen.";
//line to be coded to enable button to go back to main screen Button.E;
}
}
}
So, the offending lines of code both contain: GameObject.Find(“timerUI”).GetComponent().text. When I look in the inspector, I find that “timerUI” exists in all 3 mazes; timerUI has a property; timerUI and its properties are active in all 3 mazes. Yet it does not work in the 6 by 6 maze.
I know I am missing something, and hope extra eyes can point it out to me.
Thanks.