- Home /
destroty gui object
hi guys i want to destroy GUI object heres what i have done
using UnityEngine;
using System.Collections;
public class PowerUpScript : MonoBehaviour {
int random = 0;
float timer = 3;
// Use this for initialization
void Start () {
}
void Update()
{
if (timer >= 1 && random == 1) {
guiText.text = timer.ToString ("something");
} else if (timer >= 1 && random == 0) { // timer is <= 0
guiText.text = timer.ToString ("otherThing");
} //else
//Destroy (??);
}
void OnTriggerEnter (Collider other)
{
random = 0;
PadleScript paddleScript = GameObject.Find ("paddle").GetComponent<PadleScript>();
if(random==0)
paddleScript.AddPoints (50);
if (random == 1)
{
paddleScript.spawnball ();
}
Destroy (gameObject);
}
}
p.s im also getting a weird warning : "MissingComponentException: There is no 'GUIText' attached to the "powerUp" game object, but a script is trying to access it. You probably need to add a GUIText to the game object "powerUp". Or your script needs to check if the component is attached before using it. PowerUpScript.Update () (at Assets/Scripts/PowerUpScript.cs:19)"
thanks
Answer by RyanPaterson · Jan 05, 2014 at 06:13 PM
From what I know you can't destroy GUI. I think I attempted this not too long ago. You will probably have to use a bool to say when to display it and when to not.
So for example you could have like,
bool isShowing = false; - where you wanted to have Destroy() and: bool isShowing = true; when you initialise your GUI
EDIT: In answer to your comment
I didn't want to use your code because honestly i'm not familiar with guitext myself, so I took your update function and used it as an example to show you how the bool idea works.
*In this example GUI.Label just creates a box with text.
Here i've set the myVisibleBool to be true, although when you do it your calculations will determine whats true and whats not (which is why from what i've wrote you won't ever see the text "Other thing" because I didn't mess about with numbers for the example.
So what this does is -- When the requirements of the 'if statement' are met, it will draw some text. (same as yours; however) The bool will show true/false, in this case is like 'hide/unhide' . As I don't think you can Destroy GUI.
The update function is just something I added in there as a method of switching off and on the boolean. the myVisibleBool = !myVisibleBool just flips the value each time it's pressed.
Let me know if you're unsure, what you're unsure about. I'd recommend looking at booleans and such on youtube, theres a few basic programming ones I looked at myself quite recently, they're a big help, and very common.
bool myVisibleBool = true;
int timer = 2;
int random = 1;
void OnGUI(){
if (timer >= 1 && random == 1 && myVisibleBool == true){
GUI.Label(new Rect(10, 10, 100, 20), "Something");
}
if(timer >= 1 && random == 0 && myVisibleBool == false) { // timer is <= 0
GUI.Label(new Rect(10, 10, 100, 20), "Other Thing");
}
}
void Update(){
if(Input.GetKeyDown (KeyCode.Space)){
myVisibleBool = !myVisibleBool;
}
}
can you show me what you did i still cant find solution to my problem
I revised my answer above with the answer to your comment. Let me know if you don't understand.
thanks mate (i know what bool means btw) i just dont understand the "if" condition on update function if i understand correctly it means that only if i press the spacebar key it changed to false
Your answer
Follow this Question
Related Questions
How do I destroy a Instantiated UI image that is pushed on the Canvas? 2 Answers
[Fixed] Unity UI Text Code Reference magically dissapears 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Create Multiple Foldouts. 1 Answer