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.
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)