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 mullet123 · Apr 19, 2015 at 12:48 AM · c#destroytagcollision event

Destroy a gameobject

Hello, I am new to Unity so please be patient with me. I am trying to create a game where you have objects moving across the screen and your player character shoots projectiles. I want to make it so if the projectile hits the enemy object, the shot and the enemy will be destroyed. On both objects I used a rigid body and I gave the enemy a tag, "Enemy". In the code for the shot, I am trying to use the onCollisionEnter2D function and have it search for the object by tag. It then calls DestroyObject to destroy the shot. The code is not working as the shot passes through the enemy object. I am wondering if anyone can help me figure out a solution? The code for the shot is below.

 using UnityEngine;
 using System.Collections;
 
 public class ShotScript : MonoBehaviour
 {
     //public int damage = 2;
     public GameObject shot;
     public bool isEnemyShot = false;
     
     void Start()
     {           
     }
     void OnCollisionEnter2D(Collision2D other){
         if (other.gameObject.tag == "Enemy") {
             DestroyObject(shot.gameObject);
         }
     }
 }
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 NoseKills · Apr 19, 2015 at 04:30 PM 0
Share

On both objects I used a rigid body

Did you also put a collider on both ?

1 Reply

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

Answer by DoTA_KAMIKADzE · Apr 19, 2015 at 12:55 AM

1)If that script is a component of your shot then just:

 Destroy(gameObject);

2)If it is not then:

 Destroy(shot);

Now also make sure for both cases that your Enemy indeed have an "Enemy" tag. In order to make sure that line of code runs you can always do something like:

 if (other.gameObject.tag == "Enemy") 
 {
    Debug.Log("Found enemy");
 }

Also if it is the case #2 make sure that your shot is actually your shot, something like:

 if (other.gameObject.tag == "Enemy") 
 {
    if (shot != null) Debug.Log(shot.name);
 }
Comment
Add comment · Show 6 · 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 mullet123 · Apr 19, 2015 at 01:22 AM 0
Share

The script is a component of my shot. I made the changes as you suggested, but it doesn't seem to work.

avatar image mullet123 · Apr 19, 2015 at 04:05 AM 0
Share

If I am using RigidBody, should the collision be set to discreet or continuous?

avatar image DoTA_KAMIKADzE · Apr 19, 2015 at 11:25 AM 0
Share

Think of it like this:

Discrete - Slower/less detection checking + better performance.

Continuous - Higher/more detection checking + worse performance.

So unless your object moves really fast you should use discrete.

Now to the problem, have you checked with both Debug.Log examples I gave you? If so did the first one posted "Found enemy" and the second one posted your shot game object name(that was spawned)?

avatar image hangemhigh · Apr 19, 2015 at 01:41 PM 0
Share

If his answer solved your problem. Thank him by choosing the question as answered.

avatar image mullet123 · Apr 19, 2015 at 02:59 PM 0
Share

One question. The code is my shotscript. In unity I have the prefab for my shot. I attached the script to the prefab as a component. Should I attach the prefab to the script component or is that not necessary? Also, the script is a component of my shot and I made the changes as you suggested but it still doesn't respond.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

strange behaviour when creating and deleting objects with tags 1 Answer

Destroy object on touch of object with specific class 3 Answers

Deleting one object, not all objects at once. 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