Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jan 09 at 09:00 PM by Silver195 for the following reason:

Other

avatar image
0
Question by Silver195 · Mar 06, 2021 at 08:26 PM · bulletsclones

How do I delete a bullet clone when it touches a game object or wall?

I'm still learning Unity but I'm not sure how to delete a bullet clone when it touches a game object. I had this problem where I would put in Destroy(gameObject); when it collides with something, but when I test it, it has an error where it deletes the bullet and not the clones. How can I make it so it only deletes the clones when the clones collides with a wall or enemy?

Comment
Add comment · Show 2
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 artyomich365 · Mar 06, 2021 at 08:44 PM 0
Share

could you please share some code. I suspect that you're deleting reverence to prefab.

avatar image Silver195 artyomich365 · Mar 06, 2021 at 09:32 PM 0
Share

public class delete : MonoBehaviour {

 void OnCollisionEnter2D(Collision2D collision)
 {
     Destroy(gameObject);
 }

}

This is the code for deleting the bullet. There is a separate script for shooting the bullet if it's needed.

3 Replies

  • Sort: 
avatar image
0

Answer by Woxell · Mar 06, 2021 at 11:35 PM

gameObject refers to the object that script is attached to. Make sure you use that function in right script. Or you can use;

 void OnTriggerEnter(Collider collider){
    Destroy(collider.gameObject) //the object that collided with your character.
 }

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 Lifes_great · Mar 06, 2021 at 11:54 PM

When you mean clones are u meaning Prefabs? if so please give me both scripts you have written

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 Silver195 · Mar 07, 2021 at 12:04 AM

@Lifes_great

This is the code I used to make the bullet shoot. (got it from a Brackeys video)

public Transform firePoint; public GameObject bulletPrefab;

 public float bulletForce = 20f;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         Shoot();
     }         
 }

 void Shoot()
 {
     GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(firePoint.right * bulletForce, ForceMode2D.Impulse);
 }

 

}

Comment
Add comment · Show 5 · 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 Lifes_great · Mar 07, 2021 at 12:09 AM 0
Share

@Silver195 I'm a bit puzzled on why u want to delete the clones as well as the bullet, is there any reasoning for that?

avatar image Silver195 Lifes_great · Mar 07, 2021 at 01:33 AM 0
Share

I wouldn't have a problem with the bullet clones not deleting but I fear that it might lag a lot because of the amount of bullets and having them continuously fly forward. Do you know if the bullets not deleting will cause any lag? If they really don't cause lag then I'm ok with it but I'm not sure.

avatar image Lifes_great Silver195 · Mar 07, 2021 at 03:26 AM 0
Share

Nah they wont cause lag. unless u are making a hyper Realistic 2D game but probably best to delete them anyway

Show more comments
Show more comments

Follow this Question

Answers Answers and Comments

116 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

Related Questions

cant destroy other object 1 Answer

Deleting Bullets after a few seconds 2 Answers

Shooting with Limited Ammo 2 Answers

Need hep with pooling(call Cs in Js) 0 Answers

Getting Bullets to fire in Direction of Gun Barrel 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