I know how to play Sounds by Adding them to Audiosource and then creating script, But what if i have many different type of Characters?

Wouldn’t that mean that I’d have to repeat the long tedious process of dragging the sounds for each one of the characters. Especially i mean tedious since all the character types use the same sounds.

For example i may have 20 types of guys(different 3d model)
they all should use same sounds. Say 30 sounds.

But if i do it with the method above, then i’d have to repeat the dragging of 30 sounds onto the Audiosource pane for each character.

This is the method I"m referring to, I learned it from a Video Tutorial

  1. Add an AudioSource component to your game object
  2. Create a script . just an example. And for now only one sound, but imagine 30 or 100 sounds.

AUdioClip myClip
void start(){
audio.PlayOneShot(myclip)
}

3 Drag the script to the Game object

4 now drag the sound to the variable “my Clip” that appears in the Audio Source part of the inspector of the Game Object


So Yeah this process is cool but imagine i had 30 sounds or 100
and 20 different types of characters to do this process for.

Fortunately you are working with a computer! They're terrific at quickly performing tedious tasks. Come up with some rules, and write yourself a script that'll do this for you.

1 Answer

1

There are several ways to treat with sound in unity.
In your case if you will you the same sound for every character, like a bullet hit sound or a character getting hurt like “Aaaaahh” or whatever…
You can have a an empty game object and add Audio source + drag sound to clip variable.

Create a prefab in you project, and name it for example hitSoundObject, drag your empty GO to your prefab.

In you script,
have a public variable of type GameObject, drag your prefab to this object.
and have an another variable like private GameObject currentSoundObject.

whenever there’s a hit , you will just Instantiate your prefab, note that you have to check the “play automatically” on your audio source component.

I also use a script named “Self Destroy”, where the sound is created and destroys itsels after 1,2,3 seconds, depending on your sound.

If you need more assistance, leave a comment and I can past a code example…

hope it helps

Thanks alot. It seem complicated but i'll try that . But my simple script is not even working i dont understand why. Possibly a bug? This thing is not playing sounds AUdioClip myClip void start(){ audio.PlayOneShot(myclip) } Even though i drag my sound to the variable in the inspector. It worked before, maybe it's a bug with whole system. gonna try restarting the computer.

@greatUnityGamer i don't think its a bug with the system, you have to research why that's happening. And I'll post you some code since its really the most clear and simply way with least coding.. maybe lot of text! just a few minutes and I'll find the script

well this is an example of a raycast use, I added some comments.. and there's an another script that you will need to attach to your prefab. create a new javaScript(doesn't matter the language you use) and paste this code: public var timeToDestroy:float = 4; function Start () { yield WaitForSeconds(timeToDestroy); Destroy(this.gameObject); } drag this script to your prefab (default it will destroy itself from the 4 seconds, and its public therefor change the value from inside inspector)

Do you need any help of how creating the empty game object and dragging to the prefab? leave a comment please

That's cool. But i have a question. Those things you did using the hit.transform.tag are the enemies reacting to your Mouse click. They're not really calling up the sound themselves. this Manager script called the sounds based on the Player's Mouse input, not on the AI of the enemy So they're not creating the sounds themselves. What if i want them to create sounds themselves? like for an enemy: if( random decide to yell out something) play the YELL SOUND USED BY ALL ENEMIES Gameobjects