- Home /
I can't get Destroy(collision.gameObject) to work?
I have a coin spawner spawning coins and silver coins. The coins fly across the screen and if they hit the player, I destroy them and increment a coin counter. Below is the code attached to the player that detects collision and handles the coin count and destroying the object. If I add Destroy(collision.gameObject) to the top of the OnCollisonEnter function, it works great. If I move it down to the part where I check that the name of the object is Coin, it doesn't work. I used print(collision.gameObject.name) to make sure that the name of the object is Coin and it displays `Coin(Clone) so I'm pretty sure I have the name correct. Can anybody offer some insight as to why the objects aren't being destroyed? I assume that somehow I'm not casing out the names right, but after doing the print to check, I'm pretty sure the names are fine. Here's the code:
 function OnCollisionEnter(collision : Collision)
 {
         //Print the name of the Game Object we hit
         print(collision.gameObject.name);
       
         //If we hit a coin increment the count and destroy it
         if(collision.gameObject.name == "Coin" || collision.gameObject.name == "SilverCoin") 
         {
     
             if(collision.gameObject.name == "Coin")
             {
                 coinCount = coinCount+1;
                 Destroy(collision.gameObject);
             }
 
             else if(collision.gameObject.name == "SilverCoin")
             {
                 coinCount = coinCount+2;
                 Destroy(collision.gameObject);
             }
         }
 
         //If we hit a hazard reset the count and restart the level
         if(collision.gameObject.name == "Hazard")
         {
             coinCount = 0;
             Application.LoadLevel ("ProofOfConcept");
             hazardCount = hazardCount+1;
         }
 }
It's not a good idea to destroy collision.gameObject and still access members of it afterwards. I'm amazed it's not giving you run-time errors. 
Good point! I was trying to thin out a bunch of irrelevant stuff in this script before I posted it and it should really be after I update the coin counts for each coin type. I updated it real quick so that it makes more sense.
Thanks!
Answer by EliteMossy · Apr 04, 2013 at 12:39 AM
A quick soloution, not the best as i would probably ensure my instaniated coins do not have the (Clone).
 function OnCollisionEnter(collision : Collision)
 {
         //Print the name of the Game Object we hit
         print(collision.gameObject.name);
  
         //If we hit a coin increment the count and destroy it
         if(collision.gameObject.name.StartsWith("Coin"))
         {
              coinCount = coinCount+1;
        Destroy(collision.gameObject);
         }else if (collision.gameObject.name.StartsWith("SilverCoin")) {
             coinCount = coinCount+2;
             Destroy(collision.gameObject);
     }
      else if(collision.gameObject.name.StartsWith("Hazard"))
         {
            coinCount = 0;
            Application.LoadLevel ("ProofOfConcept");
            hazardCount = hazardCount+1;
         }
 }
So I guess this issue IS that the objects created have (clone) at the end. I'll look into trying to create them without this suffix if that's of some benefit. I tried rena$$anonymous$$g the objects after instantiating them and they still had (clone) at the end. This works great for now, thanks!
Answer by rickburgen · Apr 04, 2013 at 01:32 PM
I would just check the tag value, those I don't believe get changed when a new instance gets created.
Your answer
 
 
             Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to only delete one of two collided objects? 1 Answer
Is there anyway to make an object impenetrable? 1 Answer
Destroy GameObject With Collision Using C# 1 Answer
Ignore awake function call to destroy game object if character enters trigger 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                