• 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 /
  • Help Room /
avatar image
0
Question by MadDrax7 · Jul 13, 2018 at 09:06 AM · ontriggerenterdestroygameobject

Destroy an instance of a bullet after firing it

< I'm developing a simple 2D tank-shooting game. I have a script which uses to (and only to) instantiate and launch a bullet prefab. This script is being called by a script which attached to player named PlayerController() (when press Fire1 button for specific).>

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletBehaviourController : MonoBehaviour {
 
     public GameObject bulletLauncher;  // This is the Empty GameObject which I attached the script to
     public GameObject[] bulletPrefab;  // This is the list of prefabs of my bullet. Because my tanks have various types of bullet so I have to use a list and then, via my SystemEventHandler() script, I can change the bullet type dynamically by changing the value of typeOfBullet variable
 
     public int typeOfBullet = 1;   // This variable is uses to change the type of bullet
 
     public float force = 15f;  // This variable defines how much force to applies on the bullet's instance
 
     private void OnTriggerEnter(Collider touchedGameObject)
     {
         if (touchedGameObject.gameObject.tag == "CanBeDestroyByBullet")
         {
             Destroy(touchedGameObject);
             // I want to destroy its instance (bulletInstance) here
         }
         else if (touchedGameObject.gameObject.tag == "CantBeDestroyByBullet")
         {
             Debug.Log("Hit undstructive GameObject");\
             // I want to destroy its instance (bulletInstance) here
         }
     }
 
     public void BulletLauncher ()
     {
         GameObject bulletInstance = Instantiate(bulletPrefab[typeOfBullet], bulletLauncher.transform.position, transform.rotation) as GameObject;   // I want to destroy this instance
 
         bulletInstance.GetComponent<Rigidbody2D>().AddForce(bulletInstance.transform.up * force, ForceMode2D.Impulse); // This is use to launch the bullet's instance
 
         Destroy(bulletInstance, 5.0f);
     }
 }

The problem is: I don't know how to destroy bulletInstance in OnTriggerEnter(). Note that I have assigned Collider2D and Rigidbody2D component on every GameObject and set Collider2D of bullet prefab to Is Trigger. If need I can provide my PlayerController() script, but I don't think it's neccesary.

Thanks for your help : )

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Jul 13, 2018 at 09:25 AM

Good day.

I dont understand why don't know how to destroy the bullet... I understand this script is in the bullet right?

Then only need to add this inside the OnTriggerEnter

 Destroy (gameObjec);

So the bullet will be destroyed at the end of the frame.

Bye!

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

Answer by Skelly1983 · Jul 13, 2018 at 04:16 PM

@tormentoarmagedoom is correct as long as the script is attached to the bullet gameObject then:

 Destroy(gameObject);

Should work fine, although a better way to do this would be to use object pooling:

Object Pooling - unity3d.com

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.

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

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

Related Questions

Destroying GameObject after amount of triggers 2 Answers

My OnTriggerEnter destruction script won't trigger 1 Answer

Destroy object on hover? 1 Answer

Moving an Object Using a Trigger 0 Answers

Collider dosent detect trigger 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