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,
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;
}
}