About 'Persistent SFX'!

In this video (objectives)…

  1. Examine the different ways to ensure SFX is not destroyed when game object is destroyed.
  2. Implement PlayClipAtPoint() and reposition so that it is next to the camera.

After watching (learning outcomes)…

Trigger a SFX that persists even when the Game Object which triggered it is destroyed.

(Unique Video Reference: 30_TV_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

1 Like

I noticed that depending on the speed of the Player, the sfx on the coin could be fired off multiple times. The issue was that both colliders were able to register collisions. I fixed this by checking the collider and ignoring the BoxCollider2d (the character’s feet).

private void OnTriggerEnter2D(Collider2D collision) {
    if (collision.GetType() == typeof(BoxCollider2D)) { return; }

    AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position); 
    Destroy(gameObject);
}
2 Likes

Set coin’s game object or collider to disabled.

// Disable game object
gameObject.SetActive(false);

// Disable collider 2D on game object. Not recommened due to overhead.
// If has picked up animation, disable collider there instead.
GetComponent<Collider2D>().enabled = false;

Destroying objects is delayed until near end of frame. On the same frame, other codes may still be called and caused bugs.
NullReferenceExeption caused by Singleton is also due to this.

Privacy & Terms