Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by kalliste · Apr 03, 2015 at 12:26 AM · gameobjectdestroybeginner

Simple Way to Remove a Single Object on Click/Touch

This is, I'm certain, an incredibly dumb question. I've been trying to learn Unity, and have followed some tutorials to make some simple games like a Breakout Clone, etc.

I'm now trying to make a simple hidden object game, and I'm trying to write what should be the easiest script in the world – on tap/click I want the item that is tapped/clicked to be destroyed.

The problem is, the way I thought I would go about doing that isn't working:

 void Update () {
         
         if (Input.GetMouseButtonDown (0)) {
                 Destroy(gameObject);
         }
         
     }

When I attach this script to all of the items to be tappable/clickable, what happens is if I click any one of them, all of them vanish at once.

I would be very grateful if someone could help me with the simplest way to make it so only the object I click on disappears. I tried using 'this', but that seems to destroy the script (which makes sense), not the object itself.

Thank you so much!

I should note that this is 2D and in C#.

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

2 Replies

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

Answer by ForeignGod · Apr 03, 2015 at 12:28 AM

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     void OnMouseDown() {
         Destroy(gameObject);
     }
 }
Comment
Add comment · Show 3 · 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 kalliste · Apr 03, 2015 at 12:36 AM 0
Share

This doesn't seem to be working, and I'm sure there's something I'm messing up.

I've created the script as a C# script, and added it to each of the objects I want to be clickable/touchable as a Component. But nothing seems to be happening.

I'm not sure what might be missing. I tried adding a $$anonymous$$inematic Rigidbody 2D to the items, thinking maybe that was the problem, but that doesn't seem to have fixed it. Is there something obvious I could be missing?

Thank you so much for your patience.

avatar image kalliste · Apr 03, 2015 at 01:50 AM 1
Share

Never$$anonymous$$d, it was something I was messing up.

I didn't realize it needed to have a Collider on it.

Thank you so much for your help!

avatar image ForeignGod · Apr 04, 2015 at 12:13 AM 0
Share

Happy to help ^^

avatar image
4

Answer by Dominic.Shea · Apr 03, 2015 at 03:46 AM

         if(Input.GetMouseButtonDown(0))
         {
             Vector2 mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
             RaycastHit2D hit = Physics2D.Raycast(mousePos,Vector2.zero, 100f);
             if(hit.collider != null)
             {
                 Destroy(hit.collider);
             }
         }

Add this to your main camera, and make sure that the objects you are destroying have colliders on them.

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 axellolledoering · Feb 03, 2020 at 04:49 PM 0
Share

What if you were to do the same with touch controls??

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

6 People are following this question.

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

Destroy all GameObjects EXCEPT some... 2 Answers

Destroyed gameobject found with FindObjectsOfType<>? 1 Answer

Collect and Use Object 0 Answers

Stopping the destruction of a gameobject 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