- Home /
Destroy text
I have some GUI text on my screen that appears when the game starts. I'm trying to get it to be destroyed when you press the E button. I wrote a script to do this, but it doesn't appear to be working although I feel that its done right.
import UnityEngine
class IntroText(MonoBehaviour):
def OnGUI():
if (Input.GetKeyDown(KeyCode.E)):
Destroy(self)
the scripts in boo, and its attached to the GUitext.
I didn't put this as an answer since I'm not familiar with Boo and this might not solve your problem, but I notice one issue is that you're putting Input code in OnGUI(). This should really be in Update() ins$$anonymous$$d.
Answer by squid808 · Feb 28, 2012 at 07:01 PM
Hi there, when you are calling Destroy(self) you're destroying the script itself - not the object entirely. If you want to see this happen, you can select the object with the GUIText attached to it in the Hierarchy when running, and keep an eye on the inspector when it runs. But, as JinxM mentioned, you'll need to use Update(). As far as I know, OnGUI() is called every frame but is instead used for drawing GUI objects (for more info, see this link).
The following is assuming this is attached to a game object that has the GUIText also attached. If you want to delete the GUIText component but not the game object, you can do Destroy(self.guiText). If you want to remove the entire object, you can call Destroy(self.gameObject).
import UnityEngine
class IntroText(MonoBehaviour):
def Update():
if (Input.GetKeyDown(KeyCode.E)):
#This should remove your game object entirely
Destroy(self.gameObject)
I hope this helps out.
Your answer
Follow this Question
Related Questions
Can't destroy an arrray with GUI Text 1 Answer
Script to make a GUI texture disappear after a set amount of seconds? 2 Answers
Story Text to GUI Boxes 0 Answers
Trying to make an ASCII UI based game 0 Answers
GUI Appear Help 1 Answer