Project Boost: audio keep playing when the rocket go to the next scene

The rocket engine sound will keep playing when I finish the first level, though I keep my hand off the space bar. The code below is what I have for the rocket. Only the segment in the method processThrust() is related to audio at this stage. Is there a way to stop audio when first level is finished?

using UnityEngine;

public class Movement : MonoBehaviour
{
    // Start is called before the first frame update
    Rigidbody rigid;
    AudioSource audio;
    [SerializeField]float mainThrust=400f;
    [SerializeField]float rotationThrust=40f;
    void Start()
    {
        rigid=GetComponent<Rigidbody>();
        audio=GetComponent<AudioSource>();
        
    }

    // Update is called once per frame
    void Update()
    {
        processThrust();
        processRotation();
    }

    void processThrust(){

 
        Debug.Log(Vector3.up * mainThrust*Time.fixedDeltaTime);
        
        if(Input.GetKey(KeyCode.Space)){
            rigid.AddRelativeForce(Vector3.up * mainThrust*Time.fixedDeltaTime);
            if(!audio.isPlaying){
                audio.Play();
            }
        }else{
            audio.Stop();
        }
       

    }
    void processRotation(){
        if(Input.GetKey(KeyCode.A))
        {
            applyRotation(rotationThrust);
        }
        else if(Input.GetKey(KeyCode.D)){
            applyRotation(-rotationThrust);
        }
    }

    private void applyRotation(float rotationFrame)
    {
        rigid.freezeRotation=true;
        transform.Rotate(rotationFrame * Vector3.forward * Time.fixedDeltaTime);
        rigid.freezeRotation=false;

    }
}

Hi Charles,

Actually, when a new scene gets loaded, all game objects in the old scene get destroyed. The AudioSource should stop playing automatically. If there is a new AudioSource in the new scene, that AudioSource might play a sound itself but it won’t continue playing the sound of the destroyed AudioSource.

Do you call DontDestroyOnLoad somewhere in your code? If not, which version of Unity do you use?

Privacy & Terms