- Home /
In game message displayed on GUI - text and texture
Hello everyone,
Ive been finding answers to most of my questions here and I am therefore grateful. My game comes along very well so far. However I found myself stranded on one simple thing I cannot find answer for. I am an artist not a programmer and maybe that's the explanation right there.
Basically what I want to do is implement a simple way to display a message - text is ok, but I'd like to be able to use textures as well.
Example:
My player does this action. I want to be able to display a text and an image: "Well done" and the GUI.texture (the guy telling the player that, say, an instructor).
This is either triggered when the player has collision with some object, or the score is higher than 100.
Probably this has everything to do with triggering GUI elements but I simply cannot find a proper example anywhere.
Thanks in advance,
Answer by Rasmus Schlnsen · Oct 14, 2010 at 12:00 PM
I have created the following sample code real quick for you. Hope this is to any use. This basically triggers a GUITexture and GUIText to be displayed whenever an object collides with the object this script is attached to. It then starts a timer of guiTime seconds and then deactivates the GUI objects. Good luck!
using UnityEngine; using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GUIText myGUItext;
public GUITexture texture;
public int guiTime = 2;
void Start()
{
myGUItext.gameObject.active = false;
texture.gameObject.active = false;
}
void OnTriggerEnter(Collider c)
{
myGUItext.text = "Display msg here";
myGUItext.gameObject.active = true;
texture.gameObject.active = true;
// Start coroutine for deactivating gui elements
StartCoroutine(GuiDisplayTimer());
}
IEnumerator GuiDisplayTimer()
{
// Waits an amount of time
yield return new WaitForSeconds(guiTime);
// Deactivate GUI objects;
myGUItext.gameObject.active = false;
texture.gameObject.active = false;
}
}
1 $$anonymous$$ute took me to put it at work. Dear friend, you have my profound gratitude!
I'm probably missing something, but for every message I have to duplicate this script, right?
I'm glad you like it. You could add a public text variable and let the myGUItext.text = yourPublicVariable and then change the text for each gameobject you attach this script to. This isn't the preffered way of doing things, but if your new to coding, this might help you get your work done.
It does the job well; The problem is I cannot put a nice cartoonish bold text longer than about 7-10 words; I am thinking of some kind of break in the message to be displayed line by line, seen this somewhere on the wiki.