- Home /
How do I Enable Gui Text On Object Destroy?
I need to know how to enable the text "1/4 Notes Collected" when I left click on a sheet of paper. The paper already makes it's sound and destroy's when I click it. Now all I need to do is have the text listed above show for 3 seconds. In js, how would I do this? Here's my script. (It's very messy, I had a friends help me with it)
var information: String;
private var guiOn = false;
private var rect: Rect;
function OnMouseDown(){
guiOn = true;
rect = Rect(Input.mousePosition.x, Input.mousePosition.y, 300, 100);
yield WaitForSeconds(3);
guiOn = false;
}
function OnGUI(){
if (guiOn){
GUI.Label(rect, information);
}
}
Answer by whydoidoit · Jan 07, 2013 at 04:42 AM
I'm guessing that script is attached to the same game object as the one that is being destroyed. That's your problem - you are destroying it and still trying to use OnGUI on it! So you need to not destroy it until after 3 seconds to give the GUI time to show. You might just want to turn the renderer and collider off before that.
renderer.enabled = false;
collider.enabled = false;
This will work if it's just one object - if it is itself the parent of other objects then you will have to get all of them:
foreach(var r : Renderer in GetComponents(Renderer))
{
r.enabled = false;
if(r.collider)
r.collider.enabled = false;
}
This will work if all of your colliders are on objects with renderers - otherwise you would need a second for loop for the colliders.
(This is the script that destroys the note and plays a sound. )
var keyPickupSound : AudioClip;
function On$$anonymous$$ouseDown (){
//Play $$anonymous$$ey Pickup Sound if (keyPickupSound) AudioSource.PlayClipAtPoint(keyPickupSound, transform.position);
Destroy(gameObject);
}
So exactly - the problem is that this script immediately destroys the object - you need to get it to wait for 3 seconds - otherwise it isn't there to display the GUI
$$anonymous$$aybe you can try to disable it ins$$anonymous$$d of destroy it and a invoke could help aswell
Your answer
