Is it possible to make a gui Texture display for a certain amount of Time.
3 Answers
Try attaching the following class to the GameObject
containing your GUITexture
. The texture should have gone hidden 3 seconds after being activated.
[RequireComponent(typeof(GUITexture))]
public class GUITestClass : MonoBehaviour {
GUITexture gt;
void Awake() {
gt = GetComponent<GUITexture>();
}
void Start() {
StartCoroutine("HideGt");
}
IEnumerator HideGt() {
yield return new WaitForSeconds(3);
gt.enabled = false;
}
}
-
\$\begingroup\$ Why do you need the coroutine? \$\endgroup\$ Commented Jul 22, 2014 at 8:48
-
\$\begingroup\$ Surely this is just one of the many different ways to solve the case. \$\endgroup\$– S.C.Commented Jul 22, 2014 at 8:52
-
\$\begingroup\$ Using a threaded coroutine to simply wait 3 seconds seems overkill. \$\endgroup\$ Commented Jul 22, 2014 at 9:27
-
\$\begingroup\$ @Blue I disagree, this is a great way to deal with the problem. Using CRs is a very efficient and clean way to do things in general. It may be over kill in certain cases but what if you also wanted to play a sound when the texture is hidden? Now you have two things to do on the same 'event'. That single line Destroy now becomes useless. With a CR on the other hand you will have no headaches. \$\endgroup\$– AlexCommented Jul 25, 2014 at 6:04
You can do this with one line of code:
Destroy(gameObject, 3);
This will destroy the object after three seconds.
Put this in your script after you have sorted out your text and GUI related stuff and you're sorted.
using UnityEngine;
using System.Collections;
public class scene9 : MonoBehaviour {
// Use this for initialization
public Texture2D startWin;
public Texture2D setupWin;
private bool startwindow = true;
private bool setupWindow = false;
void Start () {
Invoke ("startwindowFun", 2);
Invoke ("setupwindowFun",3);
Invoke ("loadScene11",5);
}
// Update is called once per frame
void Update () {
}
public void startwindowFun() {
startwindow = !setupWindow;
}
public void setupwindowFun() {
startwindow = setupWindow;
setupWindow = !setupWindow;
}
public void loadScene11() {
Application.LoadLevel ("Scene11");
}
void OnGUI() {
if (startwindow) {
GUI.Label (new Rect (270, 300, 160, 100), startWin);
}
if (setupWindow) {
GUI.Label (new Rect (270, 300, 300, 100), setupWin);
}
}
}
The above code works to load a texture one after the another at a specified time.It worked for me.