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 SlyyGuyy · Mar 11, 2014 at 10:45 PM · javascriptdestroyenemyenemy aidestroy-clones

Detect collision and destroy one object

Right now in my game, I'm effectively destroying objects that are a clone of another. However, when the game spawns enemies and spawns multiple clones, if I destroy an enemy that is a similar clone before I destroy another, it deactivates the code for that object and doesn't allow me to destroy it. In simple terms, when I destroy an object that is the same kind of another object, it won't allow me to destroy the earlier instance of that object. What I'm noticing it's doing is when I hit another object with my bullets, it's finding an earlier version of the clone and destroying that instead of destroying the character that the bullet is hitting. I understand why the code is doing what it's doing, but I'm just curious as it how I can stop it from happening and have my bullets destroy the object that it's actually hitting?

 function OnTriggerEnter(collision:Collider) {
     newSeed = Random.Range(1.0, 5.0);
     
     if(gameObject.name == "Enemy_Pink(Clone)") {
         if(collision.gameObject.tag == "Bullet") {
             pinkLives--;
             if(pinkLives == 0) {
                 Destroy(GameObject.Find("Enemy_Pink(Clone)"));
                 if(newSeed == 1) {
                     spawnAirTank(0);
                 }
                 playerScore += 10;
             }
         }
     }
     
     if(gameObject.name == "Enemy_Blue(Clone)") {
         if(collision.gameObject.tag == "Bullet") {
             blueLives--;
             if(blueLives == 0) {
                 Destroy(GameObject.Find("Enemy_Blue(Clone)"));
                 if(newSeed == 2) {
                     spawnAirTank(1);
                 }
                 playerScore += 30;
             }
         }
     }
     
     if(gameObject.name == "Enemy_Red(Clone)") {
         if(collision.gameObject.tag == "Bullet") {
             redLives--;
             if(redLives == 0) {
                 Destroy(GameObject.Find("Enemy_Red(Clone)"));
                 if(newSeed == 3) {
                     spawnAirTank(2);
                 }
                 playerScore += 50;
             }
         }
     }
     
     if(collision.gameObject.tag == "Bullet") {
         Destroy(collision.gameObject);
     }
     
     totalScore += playerScore;
     Debug.LogError("Player Score: " + totalScore);
 }
Comment
Add comment · Show 1
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 getyour411 · Mar 11, 2014 at 10:50 PM 0
Share

I warned you this would happen! There's no reason to be doing all this if(gameObject.name == ....) stuff. If this is attached to each clone, then we can remove that check altogether, we can remove all the other off-color if/blue, if/pink whatever cuz that's not needed and we can remove the Find.Gobj from destroy replacing it with (this)

0 Replies

· Add your reply
  • Sort: 

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

20 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 avatar image

Related Questions

Destroy and Spawn an Enemy 1 Answer

The problem of Enemy scale when he follow the player (script) . How to fix? 0 Answers

Melle system destroy game object not working despite no errors and declining health 1 Answer

Only one cloned enemy shoots 2 Answers

Kill character on impact 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