- Home /
show guitexture on button click
i have GUI button and GUI texture. when i press gui button i want my guiTexture to show and stay on screen for 3 seconds then disappear. how would i do that with C# in unity? i am making 2D game. i have code for button and C# script code as well. right now when i click on button guitexture will only stay as long as button is pressed. but i want to stay texture on 3 seconds and then disappear.
using UnityEngine;
using System.Collections;
public class Q3 : MonoBehaviour {
public GUISkin skin = null ;
public GUITexture guiTextureWrong;
//public Texture2D wronganswer;
void OnGUI () {
GUI.skin = skin ;
guiTextureGun.enabled= false;
// first button. If it is pressed, Application.Loadlevel (1) will be executed
if(GUI.Button(new Rect(Screen.width * .05f,Screen.height * .15f,320,100), "Orange")) {
Application.LoadLevel("");
}
if(GUI.Button(new Rect(Screen.width * .05f,Screen.height * .45f,320,100), "Apple" )) {
Application.LoadLevel("Question4");
}
if(GUI.Button(new Rect(Screen.width * .05f,Screen.height * .30f,320,100), "Mango" )) {
guiTextureWrong.enabled= true;
}
if(GUI.Button(new Rect(Screen.width * .05f,Screen.height * .60f,320,100), "Banana" )) {
Application.LoadLevel ("");
}
}
}
Answer by zharik86 · Mar 08, 2014 at 07:40 PM
Simple, see my example(write on CSharp):
private float timetexture = 3.0f;
private float timing = 0.0f;
public GUITexture guiTextureWrong;
void Update() {
if (guiTextureWrong.enabled) {
timing = timing - Time.deltaTime;
if (timing < 0) {
guiTextureWrong.enabled = false;
timing = 0.0f;
}
}
}
void OnGUI() {
...
if(GUI.Button(new Rect(Screen.width * .05f,Screen.height * .30f,320,100), "Mango" )) {
guiTextureWrong.enabled= true;
timing = timetexture;
}
...
}
@ht_shh I am always glad to help. if you are familiar with the concept Coroutine, it is possible to write using it. For example, here so:
private float timetexture = 3.0f;
public GUITexture guiTextureWrong;
void OnGUI() {
...
if(GUI.Button(new Rect(Screen.width * .05f,Screen.height * .30f,320,100), "$$anonymous$$ango" )) {
StartCoroutine(this.showguitexture());
}
...
}
public IEnumerator showguitexture() {
guiTextureWrong.enabled= true;
yield return new WaitForSeconds(timetexture);
guiTextureWrong.enabled= false;
}
I write on memory so there can be inaccuracies. But I hope everything correctly.
i will try the second option you have given as well thank you for the help
Your answer
![](https://koobas.hobune.stream/wayback/20220613135725im_/https://answers.unity.com/themes/thub/images/avi.jpg)