Why is it that the sound keeps on playing even if we hit something?

My code is almost similar to Ben’s and mine too does not stop playing sound when I hit something. I know he will fix this later but I’m curious so just wanted to know that…

 AudioSource thrustSound; // for the sound
    bool playSound = false; // iff true, the music plays
    // Start is called before the first frame update
    void Start()
    {
        // initializing the rigidbody with the rocket rigidbody component
        rigidBody = GetComponent<Rigidbody>();
        thrustSound = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (state == State.Alive)
        { // if alive then process the input else dont
            Thrust(); 
            PlaySound();
        }
    }

Hi,

Does the value of state ever get set to something else but State.Alive? If not, PlaySound will get called each frame as long as the current object exists.

1 Like

I actually have. This was just a snippet of the update function. But now that I’ve seen further lectures, I understood why this was happening. I set the default sound to play whenever I thrust and that was why the sound kept on playing.
Thanks :slight_smile:

Does that mean you found an answer? :slight_smile:


See also:

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

Privacy & Terms