Destroy sound when the object is destroyed

Destroy sound when the object is destroyed

……

……

I encountered such a requirement: when the bullet touches the enemy, the bullet will disappear, and then at the same time, it will emit the sound effect of destroying the enemy.

So I put the bullet disappearing (Destroy) and the sound effect of destroying the enemy (Play) together in the collision detection function, and the result was an error.

Tried various methods can not be resolved.

Didn't fix this until I delayed the bullet's disappearance with Invoke.

This time I learned a lesson, that is, you must not allow the object to be destroyed and the sound effect to be played together, otherwise an error will be reported.

The final code is as follows:

As can be seen from the above code, it is not enough to delay the destruction of the bullet, because the bullet should disappear after hitting the enemy (instead of letting the bullet continue to move), so it is necessary to directly disable the bullet with SetActive. In this way, the player cannot see the disabled bullet, and the bullet can no longer hit any objects. This bullet is equivalent to being invalid.

Then Invoke has a special function, even if the object is disabled, it will still be called on time to destroy the object, thus completing the design requirements this time.

Guess you like

Origin blog.csdn.net/oyqho/article/details/129740153