Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change Audio Volume

Discussion in 'Audio & Video' started by DomDomDom, Feb 19, 2015.

  1. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Hello there,

    I've followed Brackey's Music Manager tutorial to create my music manager. I also followed his Main Menu tutorial to get the music slider to try and work. I'm running into some problems trying to get my music slider to change the volume in the music manager.

    Eventually I'd like to be able to control all of the music so when I run into an enemy it changes the music track, and when you go back to roaming, it changes the music track again. I'd like to also control FX sounds too. Any tips and advice on setting all of this up would be super helpful. Thank you in advance.

    Here is my GameManager Inspector:


    Here is my Music Slider Inspector:


    Here is my Music Prefab Inspector:


    Here is my GameMaster Script (I left out the player part of the script):
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GameMaster : MonoBehaviour {
    6. public static GameMaster gm;
    7. public Transform musicPrefab;
    8. public AudioSource music;
    9.  
    10. void Start () {
    11. //This starts the background song and will carry though from scene to scene.
    12. if(!GameObject.FindGameObjectWithTag("MusicManager")) {
    13. Object mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
    14. mManager.name = musicPrefab.name;
    15. DontDestroyOnLoad (mManager);
    16. Debug.Log("The Music prefab was not found and has been created");
    17. }
    18.  
    19. if (gm == null) {
    20. gm = GameObject.FindGameObjectWithTag ("GM").GetComponent();
    21. }
    22. }
    23.  
    24. public void SetMusicVolume (float vol) {
    25. music.volume = vol;
    26. Debug.Log("The music volume should be " + vol + ".");
    27. }
    28. }
    29.  
    EDIT:

    I've been trying to mess around now with the GetComponent but nothing I try seems to work.

    Code (csharp):
    1.  
    2. public class GameMaster : MonoBehaviour {
    3.  
    4. public Transform musicPrefab;
    5. private AudioSource music;
    6.  
    7. void Start () {
    8. if(!GameObject.FindGameObjectWithTag("MusicManager")) {
    9. Object mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity);
    10. music = mManager.GetComponent<AudioSource>();
    11. mManager.name = musicPrefab.name;
    12. DontDestroyOnLoad (mManager);
    13. Debug.Log("The Music prefab was not found and has been created");
    14. }
    15. }
    16.  
    17. public void SetMusicVolume (float vol) {
    18. music.volume = vol;
    19. Debug.Log("The music volume should be " + vol + ".");
    20. }
    21. }
    22.  
    MonoDevelop yells at me with error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments. It highlights the GetComponent above. I've tried several different ways to reference music with mManager and it still hates me...
     
    Last edited: Feb 21, 2015
  2. Twelv445

    Twelv445

    Joined:
    Feb 22, 2013
    Posts:
    31
    You are instantiating your musicmanager as type of "Object",
    Try instantiating it like this:

    GameObject mManager = Instantiate (musicPrefab, transform.position, Quaternion.identity) as GameObject;

    The as GameObject suffix is very important, as an object is by default instantiated as Object, but you then need to cast it as GameObject to be able to use GetComponent with it.

    Hope this helps!
     
  3. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thank you good sir! I actually just loaded it in from my main menu and told it not to destroy from scene to scene and that seems to be working. I'll try what you recommended if I run into problems in the future.