- Home /
How do I use a trigger to change the GUIText
Hi all, Im pretty new to Unity and Java scripting but I was making good progress until I hit this problem. Here is my code:
function OnTriggerEnter (myTrigger : Collider)
{
if(myTrigger.gameObject.name == "box")
{
Debug.Log("Box went through!");
var targetText = GameObject.FindWithTag("Score");
targetText.guiText.text = name;
}
}
My Debug text works, but I know I am doing something stupid with the GUI part. Can anyone help me out and point me in the right direction?
Cheers!
Answer by Tanshaydar · Sep 24, 2013 at 01:11 PM
Make sure your GameObject.FindWithTag("Score"); finds the GUItext, otherwise it will return null and won't set anything. you can check it with if(varText == null)
Alternatively, you can set your var to be a GUIText so that you can set it with targetText.text = name;
In the least, you can make it public and attach the GUIText object from hierarchy window to your script. But in order to do that, you should make your variable public.
Alternatively, you can set your var to be a GUIText so that you can set it with targetText.text = name;
Thanks Tanshaydar, I don't think my scripting is good enough to work that out though, could you give me some more clues please!?
If you declare your variable as public var targetText : GUIText;
you can see it in inspector view and assign your gameobject to it by dragging it to the area.
Thanks again. I think I need more help than I thought. Should I be using 'GetComponent' to do this? I am getting more confused...
Answer by fafase · Sep 25, 2013 at 11:07 AM
GUIText is a component attached to game object so once you have the object you still to fetch the component with GetComponent();
Your answer
Follow this Question
Related Questions
Showing text near object 1 Answer
Setting Scroll View Width GUILayout 1 Answer
How to see minutes in the timer's box? 1 Answer
Basketball Score counter 1 Answer