- Home /
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.
Answer by Nerull22 · Sep 03, 2016 at 01:57 AM
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.
Answer by b1gry4n · Sep 03, 2016 at 02:00 AM
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.
http://answers.unity3d.com/questions/411793/selecting-a-game-object-with-a-mouse-click-on-it.html
Answer by niks_goswami · Mar 11, 2018 at 01:39 AM
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?
Hi. Was a long time ago, had to re-read what the problem was. Yeah, so the above script that is the monobehaviour. The one called GameObjectClass, just attach that to the game objects that you create so that they can be monitored as well. So when you instantiate, just attach this component and it should still work.
I am getting this error in my console, I do not quite understand it?
Assets/Scripts/ButtonControl.cs(13,11): error CS1061: Type UnityEngine.UI.Button' does not contain a definition for
OnClick' and no extension method OnClick' of type
UnityEngine.UI.Button' could be found. Are you missing an assembly reference?
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
GameObject.Destroy(gameObject) does not destroy capsules 2 Answers
Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers
gameObject doesn't self-destruct after collision c# 2 Answers
References to GameObject become null 1 Answer