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
0
Question by Rick74 · Sep 03, 2013 at 12:08 PM · javascriptdestroy objectdestroy-clones

all objects sharing script get destroyed?

So I have a script that spawns an object, the object waits for a bit and destroys itself. I also wanted the functionality to click on the object, and have it destroy itself.

If I just have one, it works beautifully.

If I have more then one of these "spawners" running, and just let them wait out their lives, they each die at their unique wait time.

However, if I click on one to destroy it, all of them get destroyed?

 #pragma strict
 
 var Countdown:        int     =     5;
 var visible:         float     =     0.50;
 var invisible:        float     =     0.50;
 var blinkFor:         float    =     5.0;
 var startState:     boolean =    true;
 var endState:         boolean =    true;
 var rayDistance:     float     =     0;
 
 function Start () 
 {
     animation.Play ();
     transform.position.z = -5;
     renderer.enabled = startState;
     yield WaitForSeconds(Countdown);
     
     var whenAreWeDone = Time.time + blinkFor;
     
     while (Time.time < whenAreWeDone)
     {
         if ( startState )
         {
             renderer.enabled = false;
             yield WaitForSeconds(invisible);
             renderer.enabled = true;
             yield WaitForSeconds(visible);
         } 
         else
         {
             renderer.enabled = true;
             yield WaitForSeconds(visible);
             renderer.enabled = false;
             yield WaitForSeconds(invisible);
         }
     }
     renderer.enabled = endState;
     Destroy ( gameObject );
 }
 
 function Update ()
 {
     if (Input.GetMouseButtonDown(0))
     {
           var hit :RaycastHit;
           var ray :Ray = Camera.main.ScreenPointToRay(Input.mousePosition);              // get mouse position
   
           if (Physics.Raycast (ray, hit, rayDistance))
           {
               if (hit.transform.tag == "resource")
               {
                   Destroy ( gameObject );    
                }
            }
     }
 }

Thanks in advance for any help you can offer.

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

Answer by ICHeeryI · Sep 03, 2013 at 12:15 PM

Here is code:

  //Destroy object on click
  var hit : RaycastHit;
                var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                
                    if ( Physics.Raycast ( ray, hit, 100 ))  {
                           if ( hit.collider.gameObject.renderer && Input.GetMouseButtonUp(0)){
                                  //When user click left mouse button -select block-
                                  Debug.Log(hit.collider.gameObject);


And here is destroy part:

 Destroy (hit.collider.gameobject);

Hope it works!~

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 Rick74 · Sep 03, 2013 at 12:26 PM 0
Share

This worked perfectly! And I understand now why it was killing all of them! (just had to change the Destroy line to a capital "O");

Destroy (hit.collider.gameObject);

Thanks for the quick help! :)

avatar image
2

Answer by robhuhn · Sep 03, 2013 at 12:15 PM

I guess because all of them have the tag "resource". You should check if this is the clicked object or destroy the object which was hit.

(code not tested)

 if (hit.gameObject.Equals(gameObject))
 {
     Destroy(gameObject);
 }

or

 if (hit.transform.tag == "resource")
 {
     Destroy ( hit.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
avatar image
2

Answer by DeveshPandey · Sep 03, 2013 at 12:16 PM

Add one more condition like-

 if (hit.transform.tag == "resource" && hit.gameObject == gameObject)
 {
             Destroy ( gameObject );   
 }
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 Rick74 · Sep 03, 2013 at 12:28 PM 0
Share

thanks for the help! This would also work!

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

19 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

Related Questions

Enemy health script using raycast not destroying tagged object 1 Answer

Javascript. Respawning a character after destroyobject(). 0 Answers

Following Object can't find newly Instantiated Object (Solved) 1 Answer

How to stop Unity from destroying prefab original when destroying copied instances? 2 Answers

Difference between Destroy and Destroy immediate (In this case). 2 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