Why do my Music ignore the Sliders ?


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.

Script1 “DontDestroyAudio”:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DontDestroyAudio : MonoBehaviour
{
    private void Awake()
    {
        GameObject[] MusicObj = GameObject.FindGameObjectsWithTag("tag_BGMusic");

        if(MusicObj.Length > 1)
        {
            Destroy(this.gameObject);
        }
        DontDestroyOnLoad(this.gameObject);
    }
}

Script2 “SettingsMenu”:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SettingsMenu : MonoBehaviour
{
    private AudioSource audio_Sound, audio_Voice, audio_BGMusic;

    public GameObject ObjectMusic;
    // "0.5f" ist die startposition von den Audioslindern:
    private float BGMusicValue = 0.3f, VoiceValue = 0.3f, SoundValue = 0.3f;

    private void Start()
    {
        audio_Sound = GetComponent<AudioSource>();
        audio_Voice = GetComponent<AudioSource>();

        ObjectMusic = GameObject.FindWithTag("tag_BGMusic");
        audio_BGMusic = ObjectMusic.GetComponent<AudioSource>();
    }

    // ==================== Volume Sliders ====================
    public void SetSoundVolume(float onclick_Soundvol)
    {
    }
    public void SetVoiceVolume(float onclick_Voicevol)
    {
    }
    public void SetBGMVolume(float onclick_BGMusicvol)
    {
        Debug.Log(BGMusicValue);
        //SlinderFloat in den zwischenFloatWert: "BGMusicValue"
        BGMusicValue = onclick_BGMusicvol;
        //Zwischenwert: "BGMusicValue" in die audio Source updaten:
        audio_BGMusic.volume = BGMusicValue;
    }
}

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.

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.

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:

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

12 Likes

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.

That is AMAZINNNNG ADVICE!!! You have just helped me. I am going to use this to identify the problem. thanks mate!!

1 Like