- Home /
This question was
closed May 27, 2019 at 08:44 PM by
GroundstompStudios for the following reason:
It was the pitch xd
Question by
GroundstompStudios · May 27, 2019 at 08:37 PM ·
c#audiounityeditorsound
Unity not playing audio
My game audio isn't playing. It was earlier and then I made changes to my audio script, but it should be playing. I don't know what's happening. I even went back to my old script for it and it didn't work
using UnityEngine;
using System;
using UnityEngine.Audio;
public enum SoundTypes { ALL, SFX, MUSIC}
public class AudioManager : MonoBehaviour
{
public float masterVolume, musicVolume, sfxVolume;
public Sound[] music;
public Sound[] sfx;
public static AudioManager instance;
void Awake()
{
if(instance == null)
instance = this;
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach (Sound s in music)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
foreach (Sound s in sfx)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
void Start()
{
masterVolume = 1;
musicVolume = 1;
sfxVolume = 1;
Play("MainMenu");
}
public void Play(string name)
{
Sound m = Array.Find(music, sound => sound.name == name);
if(m != null)
m.source.Play();
else if ((m = Array.Find(sfx, sound => sound.name == name)) != null)
m.source.Play();
}
public void ChangeVolume (SoundTypes type, float newVolume)
{
if (type == SoundTypes.ALL)
masterVolume = newVolume;
else if (type == SoundTypes.MUSIC)
musicVolume = newVolume;
else if (type == SoundTypes.SFX)
sfxVolume = newVolume;
foreach (Sound s in sfx)
s.source.volume = masterVolume * sfxVolume;
foreach (Sound s in music)
s.source.volume = masterVolume * musicVolume;
}
}
Comment
Follow this Question
Related Questions
play audio for few seconds on key hit 1 Answer
Distribute terrain in zones 3 Answers
Audio listener to mono then to left or right speaker? 2 Answers
Trigger audio loop on beat with PlayScheduled 1 Answer
Play audio from directory? 0 Answers