Why do my Music ignore the Sliders ?

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

11 Likes