Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 10, 2018 at 05:30 PM by JJ_FX for the following reason:

Solved

avatar image
1
Question by JJ_FX · Oct 10, 2018 at 03:52 PM · collisioninstantiateprefab

[SOLVED] Destroy instantiated object prefab on collision

Hi,

I want to shoot bullets and once they hit objects I want them (bullets) to get destroyed (and/or trigger some more events, for particle effects mostly)

So Im shooting my bullets with this script:

 using UnityEngine;
 
 public class Shoot : MonoBehaviour {
 
     public float bulletLife = 1f;
     public GameObject bulletPrefab;
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1")) {
             GameObject bullet = SpawnBullet();
             ShootBullet(bullet);
             KillBullet(bullet);
         }
     }
 
     GameObject SpawnBullet()
     {
         var bullet = (GameObject)Instantiate(
             bulletPrefab,
             transform.position,
             transform.rotation);
         return bullet;
     }
 
     void ShootBullet(GameObject bullet)
     {
         bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
     }
 
     void KillBullet (GameObject bullet)
     {
         Destroy(bullet, bulletLife);
     }
 }

And Im using this script on a bullet prefab, hoping to destroy itself on collision:

 using UnityEngine;
 
 public class destroyOnCollision : MonoBehaviour {
 
     void OnCollisionEnter(Collision collision)
     {
         Destroy(gameObject);
     }
 }

But unfortunately it does not work. It works well on any objects that collide with bullets, but the bullet prefabs do not destroy.

Maybe Instantiate() does not instantiate scripts components? Or they are not active or sth?

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

1 Reply

  • Sort: 
avatar image
0

Answer by JJ_FX · Oct 10, 2018 at 05:30 PM

Omg... it actually works fine. I just didnt notice that I spawned my bullets too close to another collider. And my bullets were destroying too fast and I was getting weird readings in Debug.Log.

So actually its SOLVED :)

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

Follow this Question

Answers Answers and Comments

176 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 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

Prevent instantiating on collider 1 Answer

Item Drops on enemy spawn and death? 2 Answers

Error: Instantiated Enemies don't get hit 2 Answers

Instantiate Prefabs on sphere surface(mini planet) 1 Answer

BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 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