• 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 siember · Apr 28, 2020 at 05:14 PM · 2dshooting

Bullet disappears for no reason

For some reason, sometimes when the player shoots the bullet is deleted immediately after shooting. It looks as if it is colliding with something so I debug.logged it but it doesn't seem to be hitting anything that would delete it.

This is the code for shooting

     public float damage;
     public float travelLimit;
     public Rigidbody2D rb;
     public GameObject player;
 
     private void Update()
     {
         if(Vector2.Distance(transform.position, player.transform.position) >= travelLimit)
         {
             Destroy(gameObject);
         }
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         SpiderEnemyBehaviour spiderEnemy = other.GetComponent
             <SpiderEnemyBehaviour>();
         
         if(spiderEnemy != null)
         {
             spiderEnemy.TakeDamage(damage);
             Destroy(gameObject);
         }
 
         if(other != null)
         {
             Debug.Log($"Bullet hit {other.name}");
         }        
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         if (other != null)
         {
             Debug.Log($"Bullet hit {other.collider.name}");
         }
     }

 
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
0

Answer by LuigiDreemurr · Apr 28, 2020 at 05:31 PM

Does the bullet work some of the time? If so is it possible that its hitting the player or another bullet?

This is also on the bullet correct? You have a distance checker for the bullet to the player, check to see if all the variables are assigned, if not it could be thinking that its farther than it is and be destroying itself. Put a unique debug at each possible destroy point to find which one is triggering it. Good luck!

Comment
Add comment · Show 3 · 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 siember · Apr 29, 2020 at 05:35 PM 0
Share

The bullet works most of the time but even when it is working, the debug says that it is hitting the player. The script is on the bullet and i put a debug that outputs the position of the bullet when it was destroyed and there seems to be nothing wrong with it

avatar image OnEd0t siember · Apr 29, 2020 at 06:12 PM 0
Share

I ran into this before, Later i found out that the bullet spawning object i set for the bullet to spawn into on fire action was too close to the player head so i pushed the object forward a bit and it was the problem, it was hitting the player, make sure if thats the case by debug logging the name of the object that is colliding with the bullet ins$$anonymous$$d of the position.

If that is the problem, then you have two options: 1 - you push the object for instantiating the bullet further from the player (IF that's the way you set the bullet to spawn) 2 - exclude the player by a tag or a layer or even the name.

or maybe you just set it to distroy too early after spawning.

avatar image siember OnEd0t · Apr 29, 2020 at 07:40 PM 0
Share

I increased the trace limit by a bit and that managed to fix it

avatar image
0

Answer by N-8-D-e-v · Apr 29, 2020 at 06:31 PM

Like @OnEd0t said, the bullet is probably hitting the player, give your player a tag called "player", then try this

 private void OnTriggerEnter2D(Collider2D other)
      {
          if (other.gameObject.tag != player)
 {
 //destroy your object or whatever
 }
      }



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

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

2D Shooting Prefab 1 Answer

Shooting around defense position without contact 1 Answer

Shooting at an object 2 Answers

Instantiate after setting values? 0 Answers

I cant get my 2d shooting script to work? 2 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