- Home /
sending a variable to a text object
So I'm trying to figure out how to display variables ingame.
But i'm missing something because It gives me an error: NullReferenceException: Object reference not set to an instance of an object RayCaster.Update () (at Assets/Scripts/RayCaster.js:15)
#pragma strict
//import UnityEngine.UI;//do i need this? didn't seem to make a difference
var hit : RaycastHit;//holds some of the properties of an object that is detected
var output : UnityEngine.UI.Text;
function Start () { }
function Update () {
if(Physics.Raycast (transform.position, transform.forward, hit, 10)) {//if there is something 10 meters forward from the current position, also store it in hit
output.text = hit.transform.name;
}
else {
output.text = "none";
}
}
isn't output an instance of the Text object?
Also most people seem to be using C#, but Javascript seems nicer. Should I switch?
Answer by Graham-Dunnett · May 14, 2015 at 12:06 PM
If you are happy in JS then stick with JS.
At line 6 you've said that output
is a Text
. Where have you said which Text output is? You can either assign this in the Inspector for the object that uses your script, or use a look-up in the script to find the object by name.
Your answer
Follow this Question
Related Questions
UI Text showing weird string value? 1 Answer
Pause Menu Issue (Black Screen) 0 Answers
Text wont change to correct value 1 Answer
TextField text component not updating 2 Answers