i cant put text into a ui text thru a c# script
I have a problem using this code :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MouseController : MonoBehaviour {
public GameObject Crosshair;
private RaycastHit rayHit;
public Text HitsTXT;
public Text MissesTXT;
public Text ShotsFiredTXT;
public int hits;
public int Misses;
public int ShotsFired;
// Use this for initialization
void Start () {
hits = 0;
HitsTXT.text = "Hits : " + hits.ToString ();
Misses = 0;
MissesTXT.text = "Misses : " + Misses.ToString ();
ShotsFired = 0;
ShotsFiredTXT.text = "Shots Fired : " + ShotsFired.ToString ();
}
it gives the following error :
NullReferenceException: Object reference not set to an instance of an object MouseController.Start () (at Assets/MouseController.cs:20)
The weird part is that the game pauses but it does change the text but i dont want the error/crash
How do i fix this?
the error is about HitsTXT.text = "Hits : " + hits.ToString ();
when i comment it out it will go to $$anonymous$$issesTXT.text = "$$anonymous$$isses : " + $$anonymous$$isses.ToString ();
i think im missing something but i really dont know what it is.
Have you assigned those text objects? The nullreference usually means something hasn't been assigned yet.
Unless you dragged something into those inspector fields, you're calling a field from a non-existent Text object. Can you post a screenshot guaranteeing that this isn't the case?
Your answer
Follow this Question
Related Questions
Creating multiple text components on a canvas 1 Answer
In-game counter stops counting when game is built? 1 Answer
Function Problem, has been 3 hours tried to fix this.. HELP! 1 Answer
UI Text created from C# Script 0 Answers
[Beginner] [UI text] Adapt the size of the text zone to the text inside 2 Answers