My Success sound is not playing!

Hello everyone,

I added three member variables

[SerializeField] AudioClip engineSound;
[SerializeField] AudioClip blastSound;
[SerializeField] AudioClip winSound;

I have two function which gets called on collision:

  private void ExecuteSuccessSequence()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(winSound);
        Invoke("LoadScene", 2f);
    }

    private void ExecuteDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(blastSound);
        Invoke("LoadScene", 2f);
    }

Both the load scene work absolutely fine, but the winSound doesn’t play at all.
I checked the inspector, I have dragged the ‘jingle*’ file in there…
Its only when I win, it just waits for 2 sec and transitions to new scene but doesn’t play the audio. Tried tweaking the code little bit, replace the jingle sound with death sound, but still doesn’t work.

Below is my Rocket.cs code

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour {
    //todo fix lighting bug


    Rigidbody rigidbody;
    AudioSource audioSource;

    [SerializeField] AudioClip engineSound;
    [SerializeField] AudioClip blastSound;
    [SerializeField] AudioClip winSound;
    [SerializeField] float rotationThrust = 150f;
    [SerializeField] float liftingThrust = 10f;

    enum State {Dying,Alive,Transcending}
    State state = State.Alive;

	void Start () {
        rigidbody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
	}
	
	void Update () {
        if(state == State.Dying){return;}
        RespondToThrustInput();
        RespondToRotationInput();
	}

    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; } //ignore collision when dead

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                break;

            case "Finish":
                ExecuteSuccessSequence();
                break;
            default:
                ExecuteDeathSequence();
                break;
        }
    }

    private void ExecuteSuccessSequence()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(winSound);
        Invoke("LoadScene", 2f);
    }

    private void ExecuteDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(blastSound);
        Invoke("LoadScene", 2f);
    }


    private void LoadScene()
    {
        if(state == State.Dying){
            SceneManager.LoadScene(0);
        }else if(state == State.Transcending){
            SceneManager.LoadScene(1);
        }
         //todo Allow for more than 2 levels
    }

    private void RespondToThrustInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            Thrust();
        }
        else
        {
            audioSource.Stop();
        }
    }

    private void RespondToRotationInput()
    {
        rigidbody.freezeRotation = true; //take manual control of rotation
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThrust * Time.deltaTime);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThrust * Time.deltaTime);
        }
        rigidbody.freezeRotation = false;
    }


    private void Thrust()
    {
        rigidbody.AddRelativeForce(Vector3.up * liftingThrust * Time.deltaTime);
        if (!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(engineSound);
        }
    }
}

Thanks for adjusting the formatting of the content.

Any solution for this problem yet?

Hi Akshay,

You’re welcome.

Regarding the issue, seems odd, nothing jumps out at me from your code. If you would like to share your project files with me I would be happy to take a look.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Project_Boost.zip (2.8 MB)

Here is my project. Deleted the library folder. Thats a great tip I got to reduce the size without impacting much.

Let me know if you find out the root cause.

Thanks again Rob.
Regards,
Akshay Ayyanchira

Hi Rob,
wanted to share an update.

I found that in update method, I was just checking for dying state to return there itself.
if(state == State.Dying)

Now I’ve added, trasncending state to return right at the beginning along with dying.
if(state == State.Dying || state == State.Transcending){return;}

Now, I’m able to listen to sounds and the winning particle systems are also working.

Thanks for being available to me.

Regards,
Akshay Ayyanchira

2 Likes

Really pleased you found and resolved the issue yourself Akshay, always more rewarding, well done :slight_smile:

1 Like

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

Privacy & Terms