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 PotatoIng · Aug 14, 2014 at 03:42 PM · prefabdestroydelete

How to destroy GameObject when clicked on.

Hi, so I have a problem :D. I created a simple script that will instantiate a prefab of a ball on mouse coords when clicked onto the screen. Then I went on and created a second script. In the second script, wich was attached on the prefab of a ball, I tested for mouse click, when that happened, I deleted a GameObject ball. I assigned the ball prefab to the GameObject variable ball. The only problem is (also the reason why this is not a dulplicate question, atleast I hope..) that when I click on the ball it deletes all the balls. I know that its happening because they are all basically just one prefab. I thought Ill solve it by giving seperate name to each one and then deleting them by name, but Im pretty shore unity has a better solution. I dont mind javascript or c# solutions, thanks!

PS: Im using the Destroy method.

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
1
Best Answer

Answer by christoph_r · Aug 14, 2014 at 03:49 PM

That's most likely because all the balls have the script attached to it, that deletes the game object whenever a click occurs.

To make sure you only delete the ball you clicked on, you need to use some more tricks, mainly this one:

      if (Input.GetMouseButtonDown(0))
     {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      
        if (Physics.Raycast(ray, out hit))
        {
           Destroy(hit.collider.gameObject);
           print("Destroyed ball at " + Input.mousePosition);
        }  
     }

Create a separate method with that (that is NOT on every ball), and use the snippet of code in its Update method. That should work fine if the only objects in your scene are the balls you want to delete when clicked on.

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 abrar_ash · Mar 30, 2015 at 06:21 AM

Well if your are using unity 2D, then it would be definitely work for your, and make sure you have attach 2d physics collide with your prefab.

if (Input.GetMouseButtonDown(0)) { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

if(hit.collider != null) { if(hit.collider.gameObject==gameObject) Destroy(gameObject); } }

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Deleting a Prefab when it is dragged to a GUI Texture 0 Answers

Delete a prefab mesh 1 Answer

How to properly destroy object 1 Answer

i need to destroy prefab clones 1 Answer

Destroy and Spawn an Enemy 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