Hi!
Would like to ask how i could possibly have the mixer i made link to the audio source via script, i followed Brackeys tutorial on how to create a custom audio manager in unity(link for reference
) that can be used to call any sound on any part of the game , so far it works fine but when i tried to link it to the slider i made on the settings area(tutorial also by Brackeys (link for reference:
)
, i realized that on the audio source i have to point it to the master mixer so that the slider would work , apparently since the component is called after i click play theres no way for me to attach the mixer manually and have to do it by code . anyway heres my code below / Brackeys Code below. Apologies for the comments , im a beginner programmer and wanted to write notes everywhere so i could learn them better.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
// with this we will be able to use array.find to look for our indicated sound
//ugh puking inang Case sensitive yan MotherBleep
//Trying something else, puking ina
using UnityEngine.Audio;
//new function that has all Unity package wraped up in one
//goal is to have loop function and audio call / source and play method
//create a custom class for sound
public class SoundGuy : MonoBehaviour {
// Use this for initialization
public SoundGuyCC[ ] sounds;
// [ ] ← array
void Awake()
{
//same as the start method but is called right before
// goal is to loop onto a list
foreach (SoundGuyCC s in sounds)//loop to the list array
{
s.source = gameObject.AddComponent();
//component audiosource should be hosted in a variable so it can be easily called
//added s.source to point it to the wright source
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
void Start()
{
Play(“x”);
}
//play stuff method
public void Play(string name)
{
AudioMixer mixer = Resources.Load(“MasterMixer”) as AudioMixer;
SoundGuyCC s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
//where —> =>
}
}
heres the other public SoundGuyCC code
using UnityEngine.Audio;
using UnityEngine;
//this is a custom Class
[System.Serializable]
// for this to appear in the inspector need to make it serializable
public class SoundGuyCC
{
//not using monobehaviour
public string name;
public AudioClip clip;
[Range(0f, 1f)]
//slider range volume
public float volume;
[Range(.1f, 3f)]
//slider range volume
public float pitch;
[HideInInspector]//need to hide this as the source will be looped depending on
//which variable is called , even though public itll be hidden since it changes.
public AudioSource source;
}
Many Thanks!