• 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
Question by csd3535 · Jul 24, 2014 at 08:51 PM · c#destroy

Destroy an enemy without destroying the player

Fist of all I am an absolute begginer I have a 2d platformer in which I have a Mario like squishing mechanic when I jump on an enemy it dies.So I have created a box with a trigger box colider2d on to the enemy which is the child object to it and if I jump on the box first it should destroy the enemy and if touch it instead it should kill me, but instead when I jump on the box it destroyes the enemy after which it kills me as well.....which means that the enemy script is running as well

here is the box trigger script

 void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             Destroy(transform.parent.gameObject);
             Destroy(gameObject);
 
         }
     }

and here is the enemy script

 private bool fall;
 public GameObject Player;
 public Transform spawnPoint;
 public bool stomp;

 // Use this for initialization
 void Start ()
 {
     
 }
 
 // Update is called once per frame
 void Update () 
 {
     
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player") 
     {
         Application.LoadLevel(Application.loadedLevel);
     }
 }

Thank you very much !

Comment

People who like this

0 Show 0
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

Answer by Razacx · Jul 24, 2014 at 09:04 PM

When the player get's into contact with the enemy, there are some things that have to be checked and taken action upon

-If the player's y-position is higher than the enemy's y-position + a constant, destroy the enemy.

-Else destroy the player (and trigger other things like a game over screen for example).

So basically, I'd suggest doing something like this in the player OnTriggerEnter2D script:

 void OnTriggerEnter2D(Collision2D enemy) {
 
   if(transform.position.y > enemy.position.y + 0.5f) {
     //Destroy enemy
     Destroy(enemy.transform.gameObject);
   } else {
     //Destroy player and do other stuff
     Destroy(gameObject);
   }
 
 }

Comment

People who like this

0 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 csd3535 · Jul 25, 2014 at 04:55 PM 0
Share

Thank you very much for your answer but there is a problem the script gives me an error that says that in collider2d and collision2d it is not allowed to use position.....what should I do..Thanks again !

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do you destroy a clone ?c# 1 Answer

Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer

Destroy a gameobject 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