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

Question Why do my Music ignore the Sliders ?

Discussion in 'Scripting' started by LetmeDwight, Oct 24, 2020.

  1. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    Unbenannt.PNG
    When I compile the game in the editor, I can hear the music all the time (which is good too.)
    But why can't I change the volume of the BGMusic?

    Info: "snd_BGMusic" is a AudioSource where got the both scripts: "DontDestroyAudio" and "SettingsMenu" Inside of the components.
    Unbenannt2.PNG

    Script1 "DontDestroyAudio":
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DontDestroyAudio : MonoBehaviour
    6. {
    7.     private void Awake()
    8.     {
    9.         GameObject[] MusicObj = GameObject.FindGameObjectsWithTag("tag_BGMusic");
    10.  
    11.         if(MusicObj.Length > 1)
    12.         {
    13.             Destroy(this.gameObject);
    14.         }
    15.         DontDestroyOnLoad(this.gameObject);
    16.     }
    17. }
    18.  
    Script2 "SettingsMenu":
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class SettingsMenu : MonoBehaviour
    6. {
    7.     private AudioSource audio_Sound, audio_Voice, audio_BGMusic;
    8.  
    9.     public GameObject ObjectMusic;
    10.     // "0.5f" ist die startposition von den Audioslindern:
    11.     private float BGMusicValue = 0.3f, VoiceValue = 0.3f, SoundValue = 0.3f;
    12.  
    13.     private void Start()
    14.     {
    15.         audio_Sound = GetComponent<AudioSource>();
    16.         audio_Voice = GetComponent<AudioSource>();
    17.  
    18.         ObjectMusic = GameObject.FindWithTag("tag_BGMusic");
    19.         audio_BGMusic = ObjectMusic.GetComponent<AudioSource>();
    20.     }
    21.  
    22.     // ==================== Volume Sliders ====================
    23.     public void SetSoundVolume(float onclick_Soundvol)
    24.     {
    25.     }
    26.     public void SetVoiceVolume(float onclick_Voicevol)
    27.     {
    28.     }
    29.     public void SetBGMVolume(float onclick_BGMusicvol)
    30.     {
    31.         Debug.Log(BGMusicValue);
    32.         //SlinderFloat in den zwischenFloatWert: "BGMusicValue"
    33.         BGMusicValue = onclick_BGMusicvol;
    34.         //Zwischenwert: "BGMusicValue" in die audio Source updaten:
    35.         audio_BGMusic.volume = BGMusicValue;
    36.     }
    37. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Nullref has to be fixed. It's always the same approach, always.

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.
     
  3. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    I've been sitting on the problem for 5 hours today without any success!
    Every damn tutorial just adds more errors than it already has.

    I was hoping that someone can tell me the reason what is missing exactly because I have already tried everything out in this long week.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Let me take you step by step through it.

    You see the error line, line 35?

    The only possible thing that could be null in that line is
    audio_BGMusic


    So that's part 1. That's what's null. We identified it.

    Part 2... WHY is it null? Well, first we start with "who is supposed to make it not null?"

    Looking up further I see line 19 sets it, with this construct:

    Code (csharp):
    1. audio_BGMusic = ObjectMusic.GetComponent<AudioSource>();
    So lets take that apart. How can that fail? We have to suppose it DID fail, so we want to understand all the ways it can fail. Let me enumerate them, in no particular order:

    possibility A: Most common problem: this line of code never executes (put a Debug.Log() in there to see!)

    possibility B: ObjectMusic itself could be null, causing nothing else to execute

    possibility C: ObjectMusic could be valid, but there is no AudioSource on it.

    possibility D: Line 19 set it, but something else turned it back to null (always possible)

    possibility E: one of the statements in Start() coming before line 19 failed, so Start() got an exception and line 19 never ran.

    possibility X: something else maybe???

    Now if you have followed along, you have at least FIVE brand-new avenues of investigation.

    Let's take apart possibility A above. How could it not run? Here is how:

    A1. You failed to put this script on a GameObject.

    A2. The GameObject it is on was never enabled

    A3. The GameObject got destroyed before Start ran

    A4. You misspelled
    void Start()
    - does not seem like you did, but that DOES HAPPEN, you have to eliminate possibilities.

    A5 The error line 35 was executed BEFORE the Start() function runs... put two Debug.Log() statements in and find out.

    A6 ??? there could be more reasons...

    Now possiblity B: who is supposed to set ObjectMusic? Lather rinse repeat through the process... track it down methodically.

    Do you see how the thinking works here? Read the code, understand what each part actually does (this is NOT optional). Now, theorize all the myriad ways it can possibly fail, then prove to yourself that each reason is NOT why it fails.

    In five minutes of typing I have enumerated nearly a dozen different things to check on... this is how you have to think when doing software engineering. Imagine the failures and prove to yourself they didn't fail.

    You will either be left with no more reasons, OR you find the reason that is failing.

    That's how you do software engineering and troubleshooting in general
     
    Last edited: Oct 25, 2020
  5. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    The error code happens all the time, as soon as I move the slider to regulate the music volume.
    The music volume ignores constant and the slider.
    I have no idea why I don't know my way around Unity well enough for that.
     
  6. OpenGLMaster

    OpenGLMaster

    Joined:
    Jan 4, 2023
    Posts:
    3
    That is AMAZINNNNG ADVICE!!! You have just helped me. I am going to use this to identify the problem. thanks mate!!
     
    Kurt-Dekker likes this.