Correct way to play sound after disabling an object

Greetings everyone! I have been playing with collisions and sound playing after Lecture 53 of Unity 3D and I am trying to figure out the most “acceptable” or the best practice to play a sound after a collision. In my case, (Getting the PowerUp) I do the following in code:

public class PowerUp : MonoBehaviour
{
    AudioSource audioSource;
    MeshRenderer meshRenderer;
    SphereCollider sphereCollider;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        meshRenderer = GetComponent<MeshRenderer>();
        sphereCollider = GetComponent<SphereCollider>();
    }

    void Update()
    {

    }

    void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case "Player":
                print("Powered up!");
                meshRenderer.enabled = false;
                sphereCollider.enabled = false;
                audioSource.Play();
                break;
            default:
                break;
        }
    }
}

I am disabling both the Mesh Rendered and the Collider, the reason being that if I want to disable the object, then the sound won’t play. Also, no other method can be used in Update after disabling the object, so I can’t use gameObject.activeSelf afterwards.

I did also try to play the sound, then used a while loop to wait until it finishes, then disabled the object, but that crashed Unity as a whole.

This might seem like a very simple question to answer, but I am really confused here, so I just disabled those two things I mentioned above. Any other better option to play a sound?

Hi Michael,

In my opinion, it’s absolutely fine to disable the renderer and the collider. As you figured out yourself, you need an active game object to be able to play the AudioSource attached to that game object. The only thing I would change in your PowerUp class is: Remove the empty Update method. Otherwise, this empty method will get called each frame, which is a waste of resources. The rest is absolutely fine.

What exactly confused you? Is there anything you feel that needs to be improved in your solution?

Hi Nina and thanks for your response,

I had no idea that an empty Update can still play its small role (although of course it does, the program has to execute it and jump to it every frame), that is an excellent piece of information to write down, thanks!

About your question, I just felt at the time that this solution could be improved, I guess it is a personality thing. I was trying to make something work while the object was already disabled, then enabling the Audio Source temporarily to play the sound and my mind just fixed on this idea. In the end, I decided to stick with the above method and wanted to ask if it is a good practice, which after a small break and reading the small class again, seems perfectly fine.

Thanks again for your input, I really appreciate it.

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

Privacy & Terms