Forum rules - please read before posting.

Play Sound based on string variable value

edited July 2021 in Technical Q&A

Hi all,

I have a variable holding a string. This string is generated our of a conversion / players choice.
It could be e.g. "111" or "123" or "132" etc... depending on what the player has choosen to answer in a conversation.

Now I would like to play a sound based on this variable. So if the string is "111" I want a sound to be played with "111.mp3"

Yes I could do a huge action list with a lot of variable checks (if 111 play 111.mp3, etc...), but is there a smarter way?

Can I somehow assign the string to the sound clip of a sound object?

If it would be a text field in playsound I could use [var1] replacement. But play sound takes only soundclips / sound objects. As parameter I can only pass a game object. So I'm stuck.

Anyone an idea?

Thanks,
Kai

Comments

  • To play a sound by filename only - without manually linking each value variant to an asset file - you need to use Resources folders.

    If you gave each asset file a prefix e.g. "Sound_" (i.e Sound_123, Sound_111) and then placed them in a Resources asset folder, you could then play it from the scene's Default Sound through the following code:

    public void PlaySound ()
    {
        string filename = "Sound_" + AC.GlobalVariables.GetVariable ("MyVariable").TextValue;
        AudioClip audioClip = Resources.Load <AudioClip> (filename);
        AC.KickStarter.sceneSettings.defaultSound.audioSource.PlayOneShot (audioClip);
    }
    

    (Where "MyVariable" is the name of your Global String variable)

    To call this code, place inside a new C# MonoBehaviour, add to a prefab, and then use the Object: Call event Action to call the prefab's PlaySound function.

  • Thank you @ChrisIceBox. It worked fine.

    The sound I play is speech and I would like it to be affected by the games speech volume settings.
    So I tried to use different audiosource than defaultsound with sound type set to "Speech".

    public void PlaySound()
        {
            string filename = AC.GlobalVariables.GetVariable("Name").TextValue;
            AudioClip audioClip = Resources.Load<AudioClip>(filename);
            AudioSource nameSound = AC.GlobalVariables.GetVariable("AudioSourceForName").GameObjectValue.GetComponent<AudioSource>();
            nameSound.PlayOneShot(audioClip);
            //AC.KickStarter.sceneSettings.defaultSound.audioSource.PlayOneShot(audioClip);
    
       }
    

    Now I get this error message:

    Can not play a disabled audio source
    UnityEngine.AudioSource:PlayOneShot (UnityEngine.AudioClip)

    The audiosource is a preffab and part of the scene.

    Can you give a hint in the right direciton?
    Thanks

  • edited July 2021

    A Global Variable GameObject can only record prefabs - retrieving its value will always get the original prefab, not a specific scene instance.

    There's no need to use a Global Variable to store which Sound object to play from - just create an AudioSource in your script and play from that. The AudioSource can be part of a Sound object in the scene.

    public AudioSource myAudioSource;
    public void PlaySound ()
    {
        string filename = "Sound_" + AC.GlobalVariables.GetVariable ("MyVariable").TextValue;
        AudioClip audioClip = Resources.Load <AudioClip> (filename);
        myAudioSource.PlayOneShot (audioClip);
    }
    

    Alternatively, you can use GameObject.Find to get an object with a specific name.

  • Ok. Thanks.
    Is there a way to set myAudioSource Sound Type to "speech"?

  • ok. nevermind. I solved it with GameObject.Find().

    Thank you @ChrisIceBox!
    Awesome support as always!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.