using UnityEngine;
using System.Collections;
public class SelfDestroy : MonoBehaviour {
// Use this for initialization
void Start () {
Destroy(gameObject);
}
}
To the “OnClick” region of the button (screenshot attached)
Though, from some reason it doesn’t destroy the button. My problem is twofold because it should also destroy the parent text object of the button.
The Destroy() call is in the Start function, which means it destroys whatever object it’s attached to immediately when the program starts. That’s how the Start function works.
If you want a function that has to be called by another script (like the Button script), then you need to move the Destroy() call to any arbitrarily named function you want, like:
public void DestroyThisObject()
{
Destroy(gameObject);
}
Then, in the Button’s “OnClick” area, point it to the script and point it to that function, specifically.
you need to put the code into a function so the button can call it, not the Start() function.
if you want it to destroy the parent object you need to put the script on the parent object, then in the OnClick() reference the parent object and select the function you need to create to call the destruction of that parent object.
I figured I might as well put it on the start void function since the script is only activated if the button is being pressed. So what’s the harm?
So I went with a simple public void that has the destroy command in it. I put the script on the “OnClick” component of the button (in the inspector). Yet still clicking on the button causes nothing to disappear, not the button nor the parent text.
Also, be sure to turn the OnClick behaviour is actually turned on. In the screenshot it was shown as “off”. Choose “Runtime” if you only want the event to occur while the program is running (this includes when you hit “play” to preview, and is the normal setting for in-game GUI), and “Editor And Runtime” if it’s something that affects the editor / inspector itself (you likely won’t be using this option for awhile).