- Home /
Script to make a GUI texture disappear after a set amount of seconds?
Hi. I need a very simple script that will destroy a game object with a set tag after a certain amount on time set in a variable. Thank you!
If you just need any Game Object destroyed, please change your title and edit your tags to get rid of the irrelevant references.
Answer by Jessy · Jan 03, 2011 at 12:43 AM
Destroy(GameObject.FindWithTag("Tag Goes Here"), destroyDelay);
http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html
I think this would work, however I do not need to do what I wanted before, so I won't test it. Sorry. However, thank you for your guy's ideas and input! Thank you also to the one below by Unity3_User. I will give you best answer Jessy because I think that would work easier, but you both deserve points for it!
Answer by Jesus_Freak · Jan 03, 2011 at 12:26 AM
var timeToWait : float = 100.00 //this is 1 minute.
var timer : float;//this will change itself
var destroyed = false;
function Update()
{
//if(otherVar) {
timer += 1 * Time.deltaTime;
if(timer >= timeToWait)
{
if(!destroyed)
{
Destroy(gameObject.FindWithTag("GuiTexture"));
destroyed = true;
}
}
//}
}
i hope this helps, i'm using it to make a "stop game" effect. and the time and all works.
There are 60 seconds in an Earth $$anonymous$$ute, not 100. $$anonymous$$ultiplying something by 1 serves no purpose. Your code will cause errors after the first Destroy(), because it will attempt to Destroy the same object every frame, after timeToWait is up, and that object will no longer exist after the first Destroy().
it's not adding 1 seconds every seconds, it's adding 1 VALUE every frame. at the rate the frames are called, 10.00 if it was above, would be 1 second. but i agree to the 1 times anything by itself rule. 7th grade math. and the destroy function, but this script makes it somehow, that 100 = 1 $$anonymous$$ute. try it, turn the timer into a String, GUI.Box(rectInfo, timer.ToString("f0")); and see what happens. compare that to the System.DateTime.Now. then you will see how 100 = 1 $$anonymous$$ute.
Time.deltaTime is what makes that possible. and that already does the 60seconds = 1$$anonymous$$ute thing, it's adding .03 or there around, every frame, and that makes it so that the 10.00 is 1 second, 100.00 is 1 $$anonymous$$ute, and 1000.00 is ten $$anonymous$$utes.
You don't understand Time.deltaTime. Look it up in the docs to see what it means. If you still believe what you have said, after that, please post code above that supports your findings.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093739im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Delay Gameplay While Displaying 3,2,1 go!!! 3 Answers
Destroy text 1 Answer
How to draw GUI Text from code 1 Answer
How to make GUI Text appear after a certain amount of time 2 Answers
GUI Button appears when Paused 1 Answer