Success sound won't play - others will

Okay, I’ve tried everything I can think of, hopefully one of you will see what I’m missing.

My Main Engine and Death Sounds play as expected. When I land on the landing pad the game advances to the next level, but no sound plays. I’ve tried replacing the Success Sound with Thrust and Death, which work in their assigned spaces, but they won’t work for the Success event. This leads me to believe that it is the code, not the sound file itself.

I’ve made no modifications to the sound settings in Unity. I have checked the prefab and instances of my rocket. I have tried both my game scenes and my sandbox scene, same result everywhere. Apply thrust, sound. Hit obstacle, sound. Land on landing pad, no sound.

On the Prefab:
image

My Code:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour {

    Rigidbody rigidBody;
    AudioSource audioSource;

    enum State {
        Alive,
        Dying,
        Transitioning
    };

    State state = State.Alive;

    [SerializeField] float rcsThrust = 250f;
    [SerializeField] float mainThrust = 1f;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip deathSound;
    [SerializeField] AudioClip successSound;


    // Use this for initialization
    void Start () {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
        RespondToThrustInput();
        RespondToRotateInput();
	}

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

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                //Do Nothing
                break;
            case "Deadly":
                //Kill you
                StartDeathSequence();
                break;
            case "Finish":
                //Successful Landing
                StartSuccessSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        state = State.Transitioning;
        audioSource.Stop();
        print("Play Ding!");
        audioSource.PlayOneShot(successSound);
        print("Should have played ding!");
        Invoke("LoadNextLevel", 1f);    //Delay loading of next scene by 1 second. 
    }

    private void StartDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(deathSound);
        Invoke("LoadFirstLevel", 1f);    //Delay loading of next scene by 1 second. 
    }

    private void LoadNextLevel()
    {
        SceneManager.LoadScene(1);      //fix for multiple levels
    }

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

    private void RespondToThrustInput()
    {
        

        if (Input.GetKey(KeyCode.Space))
        {
            ApplyThrust();
        }
        else
        {
            audioSource.Stop();
        }
    }

    private void ApplyThrust()
    {
        float thrustThisFrame = mainThrust * Time.deltaTime;
        rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame);

        if (!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(mainEngine);
        }
    }

    private void RespondToRotateInput()
    {
        rigidBody.freezeRotation = true;  //Take manual control of rotation

        float rotationThisFrame = rcsThrust * Time.deltaTime;

        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }

        rigidBody.freezeRotation = false;  //Return control of rotation to Unity physics
    }
}

Console Output:
image

Looks like all my code needed was for me to grab a few hours of sleep, who knew?

The issue really had noting to do with my sound files, assignments, or how the sound was being played. It bug was in the Update section. Leaving out that one little IF statement there had a cascading effect on the playback of the Success sound. Ah, the joys of debugging!

	void Update () {
        if (state == State.Alive)
        {
            RespondToThrustInput();
            RespondToRotateInput();
        }
    }
1 Like

I had a similar issue where I miss typed the if statement in Update(). However, I don’t understand why that fixed it. As I understand it, OnCollisionEnter() (which is where we play the success and death sound) doesn’t get called anywhere in the RespondToThrustInput() or RespondToRotationInput() methods…so why does it suddenly work when the IF statement is there? And why did the death sound work before?

Maybe I’m not getting how OnCollisionEnter() is called?

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

Privacy & Terms