0
\$\begingroup\$

Starting with Unity having had experience of XNA im not sure if the way i want to solve my problem is the correct way. So id like the opinion of more experienced people.

I am doing a 2.5D game and have no need for 3D sounds that generate from a certain 3D position. I am used to creating a sound manager class that handles the loading of sound assets and has a static public method for each sound effect. This way it works like a toolbox i can call SfxManager.PlayDeathScreamSfx(); from anywhere in the solution.

How could i do the same in Unity?

I have tried making a game object called soundmanager and given it static methods that would play the sound effects but the SoundClips needs to be static for the static methods to be able to use them. And if the SoundClips are static then they dont show up in the inspector and i cant drag'n'drop a sound file on them to reference them.

What to do?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I am doing a 2.5D game and have no need for 3D sounds that generate from a certain 3D position

Then uncheck the 3D sound checkbox in the sound file inspector.

I have tried making a game object called soundmanager and given it static methods that would play the sound effects but the SoundClips needs to be static for the static methods to be able to use them.

I'd stay away from static stuff. In Unity you usually don't use static managers class. Instead create a SoundManager component, with a AudioSource component attached and expose public methods for playing your sound fx. For example:

[RequireComponent(typeof(AudioSource))]
public class SoundManager : MonoBehavior
{

public AudioClip soundA; //assign clips from inspector
public AudioClip soundB;

public void PlaySoundA()
{
   audio.PlayOneShot(soundA);
}

public void PlaySoundB()
{
   audio.PlayOneShot(soundB);
}
}

From the other behavior, you only need a reference to SoundManager. Assign it to them through inspector or find the GameObject with SoundManager compoents when those behaviors start.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .