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
It looks like you're new here. If you want to get involved, click one of these buttons!
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:
(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".
Now I get this error message:
The audiosource is a preffab and part of the scene.
Can you give a hint in the right direciton?
Thanks
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.
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!