Destroy a Gameobject with a UI Button

In my scene I have three Game objects and a UI Button named Destroy.

Each of the Three Shapes have a Collider, and a Highlight Script. Each of them have a simple script that Destroys the gameobject

public class Buttons : MonoBehaviour {   

    private void OnMouseDown()
    {
        Debug.Log(gameObject.name);
    }

    public void Destroy()
    {
        Destroy(gameObject);
    }
}

My question is. How can I select a Shape, then destroy it with the Destroy Button. Currently The Cube Will be destroyed because the Button’s On Click function has the Cube assigned to it.

I created this scene for simplicity, my actual project has far more game objects. So to elaborate, When I press the Destroy Button it will destroy the shape I just selected before it.

My recommendation would be to have a script attached to each game object and implement the MonoBehaviour function “OnMouseUpAsButton”. This will get called when the game object is selected like a button, which it appears is the behavior that you want. So from there you can assign a reference to each of these game objects in whichever way you see fit for your situation. You can assign the reference manually in the editor, do a GameObject.Find(), use a singleton reference or some other static call. What I may recommend personally would use a static public variable in this selection class that houses the currently selected value that the button can reference statically. Then in the button you can have a script that will get the object and destroy it. I’ll provide some simple code below for reference as my ramblings probably don’t make much sense.

Code attached to each game object:

public class GameObjectClass: MonoBehaviour
{
    public static GameObject CurrentlySelectedGameObject = null;

    private void OnMouseUpAsButton()
    {
         CurrentlySelectedGameObject = gameObject;
    }
}

So that’s the basis of your selection for a game object.
Then on the button you’ll attach a script that does something along the lines of:

public class ButtonClass : UIBehaviour
{
    [SerializeField]
    private Button _button = null;

    private void Awake()
    {
        _button.OnClick.AddListener(() => Destroy(GameObjectClass.CurrentlySelectedGameObject));
    }

    private void OnDestroy()
    {
        _button.OnClick.RemoveAllListeners();
    }
}

So that should be able to keep your scripts decoupled, you don’t need to make cumbersome .Find() calls, and it should function very smoothly. This is just one method though, there are many others, including using the button method of the monobehaviour to assign the selected to the button object itself. I hope this helped.

You need to assign the shape you want to destroy. When the user clicks a shape, have a script that passes the clicked gameobject to your destroy script. When the user presses the button, destroy it.

    public GameObject selected;

    public void SelectedShape(GameObject g)
    {
        selected = g;
    }

    public void DestroyShape()
    {
        if (selected != null)
        {
            Destroy(selected);
        }
    }

Selecting a shape can be achieved a few ways. The easiest way to understand (i think) is to cast a ray from the mouse position on your camera into 3d space. If it hits a shape, pass that to your destroy script.

Hi,
I actually want to delete a selected object from the scene. However, that object is instantiated from another script. So how may I attach the above scripts to an object that doesn’t exist just yet?