- Home /
Trying to access GUI.Label text
I'm trying to change the GUI.Label text in C# and it seems to be working fine; however, when attached to different objects it's almost like each object is creating a separate instance of the GUI.Label. Each click of a different object changes the text and clears it after the 5 second invoke as it should but clicking another object overlays the new text instead of clearing it...again as if it's creating a new instance. I had a version where I created the new protected Rect and set it in Start() but it was doing the same thing.
Does anyone know the C# dot reference to dynamically created GUI.Label items? or if it is creating instances of the same GUI.Label to basically clear all of them out before setting the text again? i tried searching but nothing seems to be coming up in reference to this. Would it be creating new instances of the GUI.Label or instances of the text inside?
Using 2 scripts EventManager.cs and inven.cs (where the onClick is called)
EventManager.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EventManager : MonoBehaviour {
protected string thisString = "";
private GUIStyle myStyle = new GUIStyle();
private RectOffset rctOff;
private int textWidth = Screen.width - 50;
private int textHeight = 10;
protected int textBoxTop = 30;
protected void displayMessage(string displayString, int lineCount) {
thisString = displayString;
textBoxTop = textHeight * lineCount;
CancelInvoke();
Invoke ("removeText", 5);
}
void removeText() {
thisString = "";
}
void OnGUI() {
Font thisFont;
thisFont = Resources.Load<Font> ("Japonesa_0");
myStyle.font = thisFont;
myStyle.fontSize = 20;
myStyle.normal.textColor = Color.white;
myStyle.alignment = TextAnchor.MiddleCenter;
myStyle.wordWrap = true;
myStyle.fixedWidth = textWidth;
myStyle.fixedHeight = textHeight;
myStyle.name = "textBox";
rctOff = GUI.skin.customStyles[0].overflow;
myStyle.overflow = rctOff;
GUI.Label (new Rect (Screen.width / 2 - textWidth / 2, textBoxTop, 100, 30), thisString, myStyle);
}
}
inven.cs using UnityEngine; using System.Collections;
public class inven : EventManager {
void OnMouseDown () {
base.displayMessage ("You clicked " + this.name + " ~ " + Random.Range(-10.0F, 10.0F).ToString(), 1);
}
}