- Home /
How do I change the text of a GUIText object through another GameObject using a variable?
The title might be a little hard to understand so basically this is what I want to do:
I have a GUIText object and I want to put a script on a GameObject (cube). This script will have a variable like var EnemyName = "";
I want to put that script on that GameObject (cube), so I can set a particular text for the GUIText object through the inspector for the cube. I will be doing this for more than one GameObject.
How do I do this?
I have no evidence for this but out of the blue i think you would have to reference the GUIText object and then set the GUIText.text variable to enemy name However way simpler would be to just use OnGUI() and then use a GUI Label inside that
Answer by e-bonneville · Feb 03, 2011 at 10:06 PM
Here's an untested JS version of what you could do:
var label : GUIText; var enemyName = "";
function Update() { label.text = enemyName; }
You'd put that script on your cube and assign your GUIText to the variable Label in the Inspector. After that the GUItext will always equal the enemyName variable on the cube.
Hope this helps!
I'm trying to do this, but the script which has access to the variable I need (in the example above it would be enemyName) is a script which is not attached to an object. How would I do it in that case? I need a way to reference the field
Answer by lCmt · Oct 04, 2013 at 03:26 PM
Hey guys,
I'm doing it in C# and I can't get it right, I'm getting this error all the time:
error CS1955: The member `UnityEngine.GUIText.text' cannot be used as method or delegate
Here's my code
[System.Serializable]
public class ViewScrollZoom : MonoBehaviour {
public GUIText debugger;
void Start() {
}
void Update () {
if(Input.touchCount == 1){
debugger.text("Touching screen now");
}
}
EDITED
Sorry for being stupid. I was trying to figure it out for at least an hour, and finally saw it in front of my eyes... The good part is that I couldn't find this error referenced anywhere so I'll just leave it here for future intelligent crowd..
It should be debugger.text = "Clicking with mouse left ";