AudioClips

I can’t seem to get the Die and Win audioclips to work. The console gives no error.
I’ve put the audioclips in the serialized fields and i feel like i’ve triple checked the code in the lecture now.

I also saw a different post where the audioclips from the movement.cs file had to be called on outside of the update() function but i already have that.

Could this be a version issue? i’m working in 2019.4.18f1.

regardless thanks in advance for anyone able to help :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
    [SerializeField] float gracePeriod = 1f;
    [SerializeField] AudioClip Bonk;
    [SerializeField] AudioClip Winnar;


    AudioSource audioSource;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }
    void OnCollisionEnter(Collision collision)
    {

     

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("You hit a friendly object");
                break;
            case "Finish":
                FinishLevel();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

    private void FinishLevel()
    {
        //todo add sfx on finish level
        audioSource.PlayOneShot(Winnar);
       
        //todo add vfx on finish level
        StopMovement();
        Invoke("LoadNextLevel", gracePeriod);
    }

    void StartCrashSequence()
    {
        //todo add sfx on crash
        audioSource.PlayOneShot(Bonk);
        
        //todo add vfx on crash
        StopMovement();
        Invoke("ReloadLevel", gracePeriod);
    }

    void ReloadLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }

    void LoadNextLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }

    void StopMovement()
    {
        GetComponent<Movement>().enabled = false;
        GetComponent<AudioSource>().Stop();
    }

}

I looked at someone else’s solution that used the audioSource.Stop(); function before calling the audioSource.PlayOneShot(); but this didn’t work for me either. The mainthrust sound works fine and i can hear the audio when listening to them in the project folder.

Hi Woutarr,

What’s the value of gracePeriod during runtime? Does the next scene get loaded immediately or is there a delay, so the audio source has got enough time to play the clip?

Regarding the version of Unity, it is recommended to use at least the same as Rick.

1 Like

Hey Nina thanks for helping out :slight_smile:

It was 1 second but i put it up to 5 and i didn’t notice any changes, so i don’t think it’s that unfortunately.

I tried changing up the method a bit but it also doesn’t seem to help.

private void FinishLevel()
    {
        //todo add sfx on finish level

        if (audioSource.isPlaying)
        {
            audioSource.Stop();
            audioSource.PlayOneShot(Winnar);
        }
        else 
        {
            audioSource.PlayOneShot(Winnar);
        }
        
        //todo add vfx on finish level
        StopMovement();
        Invoke("LoadNextLevel", gracePeriod);
    }

    void StartCrashSequence()
    {
        if (audioSource.isPlaying)
        {
            audioSource.Stop();
            Debug.Log("stopped playing ealier audio");
            audioSource.PlayOneShot(Bonk);
        }
        else
        {
            audioSource.PlayOneShot(Bonk);
        }
        //todo add vfx on crash
        StopMovement();
        Invoke("ReloadLevel", gracePeriod);
    }

Add a Debug.Log to FinishLevel and the if and else block with meaningful messages so you will be able to distinguish them from one another. Do the same for StartCrashSequence.

Also try to call audioSource.PlayOneShot(Bonk); in Start to see whether the sound gets played. If it does not get called either, it might be that the problem is not in the code but somewhere else.

Ok so i put the audioclip as a PlayOneShot(Winnar); in the Start and i occasionally hear the first 0.1 second of the audioclip, but like i said it’s inconsistent. :face_with_raised_eyebrow:

It seems to play with more certainty if i switch between my headset and speakers as audio output before running the game. So in conclusion it seems i can hear (a tiny tiny fraction) of the audioclip when i put it in Start.

I also added more debug.logs to the methods and the if else statements seem to pick up the correct values.

I added a screenshot of my project settings as it might be that?

image

Edit: I also tried other sound effects from a free asset but this didn’t seem to do anything for me

ok so I figured it out and I gotta say I feel a bit silly, it turns out I put the StopMovement() method after the PlayOneshot lines.

  void StopMovement()
    {
        GetComponent<Movement>().enabled = false;
        GetComponent<AudioSource>().Stop();
    }

Now the SFX are after the StopMovement() method so they work, even though the one in start still seems to be acting off but that’s a thing for another time I suppose

    void StartCrashSequence()
    {

        StopMovement();
        Invoke("ReloadLevel", gracePeriod); 

        if (audioSource.isPlaying)
        {
            audioSource.Stop();
            Debug.Log("stopped playing old audio and play Bonk audioclip");
            audioSource.PlayOneShot(Bonk);
        }
        else
        {
            Debug.Log("play Bonk audioclip");

            audioSource.PlayOneShot(Bonk);
        }
        //todo add vfx on crash
     

There’s propably a more elegant solution but for now I’m just happy it works :grin:

Thanks for helping me out Nina

Awesome! I’m glad you fixed the issue. :slight_smile:

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

Privacy & Terms