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 imnickb · Apr 04, 2013 at 12:30 AM · collisiongameobjectdestroyspawncoin

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;
         }
 }
Comment
Add comment · Show 2
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 Unitraxx · Apr 04, 2013 at 12:34 AM 0
Share

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.

avatar image imnickb · Apr 04, 2013 at 12:36 AM 0
Share

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!

2 Replies

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

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;
         }
 }
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 imnickb · Apr 04, 2013 at 01:39 AM 0
Share

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!

avatar image
1

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.

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

13 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

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


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