Why the Bullet is not destroyed when touching the Walls of the platform?

I have been trying to get the bullets disappear when they hit the walls. Enemy and bullet both are destroyed when it is a Trigger, but the bullet does not disappear when it is a collision. Look at the following image, c# script and advice.

public class Bullet : MonoBehaviour
{

[SerializeField] float bulletSpeed = 10f;

Rigidbody2D myRigidbody;
PlayerMovement player;
float xSpeed;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    player = FindObjectOfType<PlayerMovement>();
    xSpeed = player.transform.localScale.x * bulletSpeed;
}


void Update()
{
    myRigidbody.velocity = new Vector2(xSpeed, myRigidbody.velocity.y);
}



void OnCollisonEnter2D(Collision2D other)
{
    Destroy(gameObject);
}


void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Enemy")
    {
        Destroy(other.gameObject);
    }

    Destroy(gameObject);
}

}

Hi,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?


See also:

If the part of the code you show is from the bullet prefab, you should

void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Enemy")
    {
        Destroy(other.gameObject);
    }

    Destroy(this);
}

instead of gameObject.

Disclaimer: I am not following this course, but program in Unity.

No, this is not correct. You want to destroy the gameObject. If you destroy this you only destroy the component and the gameObject will stay behind.

@shiva_narayana Does the bullet have a collider that is not marked as trigger?

@bixarrio , you are right. Looking at the Unity documentation they say the following:

    void DestroyGameObject()
    {
        Destroy(gameObject);
    }

    void DestroyScriptInstance()
    {
        // Removes this script instance from the game object
        Destroy(this);
    }

In any case, I saw a looked at the wrong function as well. The poster mentioned the OnCollision and I commented on the OnTrigger which is my mistake, so please ignore my feeble attempt :slight_smile:

@MarcelTb It’s all good, we all learn.

Yes, the bullet has Capsule collider 2D which is not marked as trigger.

So, have you followed @Nina’s advice and added debug logs to OnCollisionEnter2D? Does it log anything?

image

yes I have used Debug.Log(other); at OnTriggerEnter2D method and have taken the screen shot of the results.

I have also used the Debug.Log(other); at OnColliderEnter2D method and have taken the screen shots of the results.

This is my current c# script for Bullets

ok, I can see it logging something, but I don’t know what. I don’t see any enemies on the screen, so I can only assume that it is from OnTriggerEnter2D and not from OnCollisionEnter2D. So, nothing is hitting OnCollisionEnter2D. You want to know if something is hitting the methods. Putting debug logs in all the methods and having them all print the same thing doesn’t help much.

OnTriggerEnter2D works, so remove the debug logs from there. You don’t need it. Can you do that and run it again? Then, also show the inspector for Bullet. I would like to see the colliders on the bullet object

As you suggested, I have removed Debug.Log from OnTriggerEnter2D and found out my OnCollisionEnter2D method is not working. Do you suggested changing the physical size of capsule colliders 2D on both bullet and enemies ?

You could make the colliders of the bullets and enemies significantly larger. If they are very small, they could be missed by fast moving objects. Or they could miss other colliders if they are small. For reasons of performance, Unity does not check everything each frame. And game objects do not move but teleport each frame. The smooth movement is an optical illusion.

By the way, could there be a typo in the OncollisionEnter2D method? I think there is an “i” missing.

2 Likes

Wow! Good find, @Nina

Thank you so much Nina and Bixarrio.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms