Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by birns92 · Sep 03, 2016 at 01:23 AM · c#gameobjectbuttondestroy

Destroy a Gameobject with a UI Button

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

alt text

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.

destroy1.jpg (145.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image birns92 · Sep 03, 2016 at 02:49 AM 0
Share

Thank you, very simple and easy to understand.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerull22 · Mar 11, 2018 at 07:44 PM 0
Share

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.

avatar image niks_goswami_unity · Mar 12, 2018 at 11:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

224 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges