NullReferenceException on Text element
I have a script which, among other things, accesses a UI text element to display a timer that is constantly counting down. When the player interacts with certain game objects the timer goes up. For reasons unknown to me when I try to access the setTimer function (what sets the text in the UI text element) from the OnTriggerEnter function (where i deal with what happens when the player hits an object) it says the timerText object is null, however when i access it from the Start function and the Update function it works properly. Not sure whats going on here. (note the rocket boost functionality is also not working but thats not what im struggling with right now).
Heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class gameController : MonoBehaviour
{
//Values that are applied to UI elements
private float gameTime;
private int score;
//UI Text Elements
public Text scoreText;
public Text timerText;
public Text welcomeText;
//collectable prefabs
public GameObject hourGlass_prefab;
public GameObject money_prefab;
public GameObject rocket_prefab;
//values for rocket boost logic
private bool isBoosted;
private float boostTime;
//variables for player movement
Vector3 movement;
public float distance = 20f;
new Rigidbody rigidbody;
// Start is called before the first frame update
void Start()
{
//set welcome test and initialize variables
welcomeText.text = "Welcome to Spaceship Arcade! \nCollect the rings to score points!";
isBoosted = false;
score = 0;
setScore();
setTime(20.0f, true);
gameTime = 20;
Destroy(welcomeText, 3.0f);
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//move player in camera direction
transform.position = transform.position + Camera.main.transform.forward * distance * Time.deltaTime;
//count down timer with system time
setTime(Time.deltaTime, false);
//logic for ending rocket boost
if (isBoosted)
{
if(gameTime - boostTime >= 10.0f)
{
distance = distance - 10.0f;
isBoosted = false;
}
}
}
//checks what object is collided with and acts accordingly
private void OnTriggerEnter(Collider other)
{
Debug.Log("Object entered trigger");
if (other.gameObject.name.Contains("score_ring"))
{
Debug.Log("Score_ring collided");
//spawns next score ring
GameObject newRing = Instantiate(other.gameObject);
//determines if a bonus collectable will be spawned and what type,
int rand = Random.Range(0, 10);
if(rand >= 8)
{
GameObject hourglass = Instantiate(hourGlass_prefab);
hourglass.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-10.0f, 10.0f), other.gameObject.transform.position.z + Random.Range(20.0f, 35.0f));
}
if(rand <= 2)
{
GameObject rocket = Instantiate(rocket_prefab);
rocket.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-10.0f, 10.0f), other.gameObject.transform.position.z + Random.Range(20.0f, 35.0f));
}
if(rand >2 && rand < 6)
{
GameObject money = Instantiate(money_prefab);
money.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-10.0f, 10.0f), other.gameObject.transform.position.z + Random.Range(20.0f, 35.0f));
}
newRing.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-20.0f, 20.0f), other.gameObject.transform.position.z + Random.Range(40.0f, 70.0f));
score += 10;
setTime(5.0f, true);
setScore();
Destroy(other.gameObject);
}
//hourglass gives points and extends timer
if (other.gameObject.name.Contains("hourglass"))
{
Debug.Log("Hourglass Collided");
// setTime(10.0f, true);
score += 5;
setScore();
Destroy(other.gameObject);
}
//moneybag gives large point boost and nothing else
if (other.gameObject.name.Contains("moneybag"))
{
Debug.Log("Moneybad Collided");
score += 20;
setScore();
Destroy(other.gameObject);
}
//rocket gives movement speed boost
if (other.gameObject.name.Contains("roc"))
{
Debug.Log("RocketCollided");
distance = distance + 10.0f;
isBoosted = true;
// boostTime = gameTime;
Destroy(other.gameObject);
}
}
//sets score on UI
public void setScore()
{
scoreText.text = "Score: " + score.ToString();
}
//sets game time on UI (if pos is true then adds an amount, else subtracts)
public void setTime(float time, bool pos)
{
float temp = gameTime;
if (pos)
{
gameTime = temp + time;
}
else
{
gameTime = temp - time;
}
if (timerText != null)
{
timerText.text = gameTime.ToString() + " seconds";
}
else
{
Debug.Log("helpful error message");
}
}
}
Answer by Vollmondum · Apr 06, 2019 at 05:05 AM
timerText is a text component, not an object. Use timerText.gameObject
Your answer
Follow this Question
Related Questions
Accessing UI text 0 Answers
How do I keep two UI text unity3d on the scene 2 Answers
UI Text created from C# Script 0 Answers
UI Text Not Updating 1 Answer
Changing the UI Text 0 Answers