Finish Audio clip not playing new scene loads

I am having Issue regarding finishing Level clip loading.Both thrust and Death audioclip works perfectly well but once it reaches the landing Pad, it waits for the seconds specified and transcend to next scene but fails to play the audioClip.

Here is a video demo of the game play


I’m not sure where the bug is from but. I’m sure the StartFinishSequence() function loads Next Level without issue.

I assigned necessary audio as well in the inspector
image

Below is my Rocket.cs code

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{
    // Rigid Comp
    private Rigidbody m_rigidbody;
    AudioSource audioSource;

    //Variables
    [SerializeField] float mainThrust  = 100f;
    [SerializeField] float rcsThrust = 100f;
    //AudioClips
    [SerializeField] AudioClip mainThrustClip;
    [SerializeField] AudioClip finishLevelClip;
    [SerializeField] AudioClip deathExplosionClip;
    
    enum State { Alive, Dead, Transcending};
    [SerializeField] State state = State.Alive;

    // Start is called before the first frame update
    void Start()
    {
        m_rigidbody = GetComponent<Rigidbody>(); // get the Rigid Comp for ship
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        // todo stop sound on death
        if (state != State.Dead)
        {
            Thrust();
            Rotate();
        }
    }

    private void OnCollisionEnter(Collision collision)
    {

        if (state != State.Alive) return; // Ignore collision when dead or transcending

        string tag = collision.gameObject.tag;

        switch (tag)
        {

            case "Friendly" :
                // do nothing
                break;

            case "Enemy":
                StartDeathSequence();
                break;

            case "Finish":
                StartFinishSequence();

                break;

            default:
                Debug.Log("I'm Not tagged");
                break;
        }
    }

    private void StartFinishSequence()
    {
        state = State.Transcending;
        audioSource.Stop(); // stop any other audio
        audioSource.PlayOneShot(finishLevelClip);
        Invoke("LoadNextScene", 2f);
    }

    private void StartDeathSequence()
    {
        state = State.Dead;
        audioSource.Stop(); // stop any other audio
        audioSource.PlayOneShot(deathExplosionClip);
        Invoke("LoadBeginningScene", 2f);
    }

    private void LoadBeginningScene()
    {
        SceneManager.LoadScene(0);
    }

    private void LoadNextScene()
    {
        SceneManager.LoadScene(1);
    }

    private void Thrust()
    {
        if (Input.GetKey(KeyCode.Space))
        {
             // Applying thrust
            m_rigidbody.AddRelativeForce(Vector3.up * Time.deltaTime * mainThrust * 10);

            if (!audioSource.isPlaying )
                audioSource.PlayOneShot(mainThrustClip);
            
            
        }
        else
        {
            audioSource.Stop();
        }
        
    }
   
    private void Rotate()
    {
        // pause RB rotation force while performing our own
        m_rigidbody.freezeRotation = true;
        
        
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(Vector3.forward * Time.deltaTime * rcsThrust);
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(-Vector3.forward * Time.deltaTime * rcsThrust);
        }
        // Resumes RB rotation force
        m_rigidbody.freezeRotation = false;
    }

  
}

I wil appreciate if any help can be rendered

Hi Adeoye,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? If so, please add a few Debug.Logs to your code to see whether the methods get executed as expected. Are there any error messages in your Console while you are playing your game?

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

Privacy & Terms