• 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 /
  • Help Room /
avatar image
0
Question by AutoSkyPilot · Sep 12, 2016 at 11:44 AM · unity 5gameobjectscript.scripting beginnergameobjects

Destroy(this.gameObject) destroys all the obejcts in the scene, i want only the one that is clicked on

I want to destroy gameobject when they are clicked on, but right now when i click on one all the gameobjects in the scene destorys, how do i fix this?

 void Update ()
     {
         ClickedOnAction();
 
     }
 
     void ClickedOnAction()
     {
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
            Destroy(gameObject);
         }
         
     }

This is the code i have right now. And if it has any difference, i have many of the same objects in the scene so how do i fix this?

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

1 Reply

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

Answer by ScaniX · Sep 12, 2016 at 11:47 AM

Your code contains no condition other than mouse pressed, so every gameObject containing this script will of course be destroyed.

If your object has a collider, you can use the event method OnMouseDown() or OnMouseUpAsButton() (more similar to a click on that object) and add the line Destroy(gameObject) there.

This is the whole script for this:

  void OnMouseUpAsButton()
  {
     Destroy(gameObject);
  }

Make sure to give your object a collider (assuming it is a 3D object). Also see here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUpAsButton.html

Comment
Add comment · Show 5 · 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 AutoSkyPilot · Sep 12, 2016 at 12:37 PM 0
Share

Cant realy get it to work, can you be a bit more specific on how to fix it?

avatar image AnthonyHJ-SAH AutoSkyPilot · Sep 12, 2016 at 01:25 PM 1
Share
  1. Add a collider if your objects do not have one.

  2. Replace everything you have with the following:

void On$$anonymous$$ouseDown()
{
    Destroy(gameObject);
}

No need for updates and checking and all of the other stuff; On$$anonymous$$ouseDown() checks whether the cursor is over the object (even if the cursor is hidden and sitting at the centre of the screen) and then destroys it if so.

avatar image AutoSkyPilot AnthonyHJ-SAH · Sep 12, 2016 at 01:50 PM 0
Share

I kinda know what you are talking about but now when i run the function the objects dosent even come on to the sceen, they gets destroyed even before they enter beacuse i have On$$anonymous$$ouseDown in update. void Update () {

         On$$anonymous$$ouseDown();
 
     }
 
     void On$$anonymous$$ouseDown()
     {
         Destroy(gameObject);
     }



Show more comments
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Set the username with an inputfield ? 3 Answers

Is it safe to instantiate a GameObject and then change it's components? 1 Answer

how to assign different Gameobject to a single variable during run time using c#? 1 Answer

Unity5/C# beginner here! Need some scripting help :) <3 1 Answer

GetComponent is not a member of Object 0 Answers


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