1

I did code to do volume slider for my game i and i ran into problem with unity

I tried alot of codes and none worked and i need someone to explain me why its giving me error and how could i fix it

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

public class OptionsMenu : MonoBehaviour
{

    public AudioMixer mixer;

    public void SetLevel (float sliderValue)
    {
        mixer.SetFloat("volume", Mathf.Log10(sliderValue) * 20);
    }

}

it should change my master mixer as i change the slider

Christopher
  • 9,634
  • 2
  • 17
  • 31
ItzGuy
  • 9
  • 3
  • Is the error on the mizer.SetFloat line and has mixer been instantiated? – d219 Nov 01 '19 at 17:21
  • 1
    @d219: I would guess it is that line. And it is not initlaized in this code. – Christopher Nov 01 '19 at 17:25
  • 1
    the error is "UnassaginedReferenceException" and it says the variable mixer of OptionsMenu has not been assigned. – ItzGuy Nov 01 '19 at 17:28
  • 2
    It's right. The variable `mixer` is not assigned in this code. – Ian McLaird Nov 01 '19 at 17:30
  • @EpicOGnamEYT: Wich is entirely true and entirely understandable. What is your question? – Christopher Nov 01 '19 at 17:31
  • my question is how i solve this error – ItzGuy Nov 01 '19 at 17:41
  • 2
    You solve an unassigned reference error by assigning a reference. – 3Dave Nov 01 '19 at 18:55
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Draco18s no longer trusts SE Nov 01 '19 at 19:27
  • @Draco18s: **How** is this a Duplicate of that Answer? It is not the right exception at all. And indeed the big issues was figuring how to get a instance of this class, as it is "it's complicated" case. – Christopher Nov 01 '19 at 20:17
  • @Christopher Unity uses C# wrapper objects around the underlying C++ objects. The error produced when the C# object is non-null and the C++ object is null is not a literal NullReferenceException, but a Unity-generated MissingReferenceException or UnassignedReferenceException depending on whether or not you destroyed a thing or never created a thing. Both are still, fundamentally, null reference errors. – Draco18s no longer trusts SE Nov 01 '19 at 23:09
  • @Draco18s: I am actually pretty sure the "Variable not Initialized" error is a Compiler error. Based on Code analysis, before the thing is even compiled. But "NullRefenrence" is a runtim exception. A very different kind of beast. Like trying to reassign a readonly value later or calling a private function. – Christopher Nov 02 '19 at 00:19
  • @Christopher You are referring to the Visual Studio compiler error "Use of unassigned variable..." which is not applicable here, because the `mixer` is a field in a class, not a local variable, so Visual Studio doesn't know if it is being given a value somewhere else (yes, even if it is private, it isn't until you apply the `readonly` modifier that VS will know to complain). [Reference capture](https://i.stack.imgur.com/FxRRr.png) (note that `mixer` is not underlined in red). So no, that is not the asker's issue. – Draco18s no longer trusts SE Nov 02 '19 at 01:17
  • @EpicOGnamEYT - I hope, [this answer](https://stackoverflow.com/a/65267967/10819573) fulfilled your requirement. If not, please comment below the answer so that I update it. If yes, you can help the community by marking it as accepted. – Arvind Kumar Avinash Dec 12 '20 at 19:17

1 Answers1

-1

Every variable has to be assigend a value/initialized, before you can use it.

Older/closer to the metal languages like native C++ would not have such a warning, and thus use whatever data happened to be written in that memory location by pure chance. Wich was never something sensible. C# simply will not compile if it detects such a mistake.

I am going to take a guess that AudioMixer is a class, so you need to create a instance to use it:

public AudioMixer mixer = new AudioMixer();

However there are special cases like Factory Pattern, and I have to look up wich approach AudioMixer uses.

Edit: Assuming I got the right AudioMixer class, it appears to be a singleton. But I can not find it's factory method: https://docs.unity3d.com/ScriptReference/Audio.AudioMixer.html

Edit2: I can not find a non-Designer way to create a AudioMixer instance online. So I added the Unity3D tag.

Edit3: Credit to 3Dave for this: "AudioMixer is a design-time class tied to the Audio Mixer window in the editor. It should never be instantiated or dynamically created."

So it is indeed never supposed to be created by anything but the designer. And thus a variable in code makes not a lot of sense.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • 1
    @EpicOGnamEYT: Nope. "mixer" is the variable. "public" how accessible it is. "AudioMixer" is the type of the variable, and propably a classname. – Christopher Nov 01 '19 at 17:27
  • can you help me solve the error? its says the variable has not been assigned – ItzGuy Nov 01 '19 at 17:33
  • 1
    @EpicOGnamEYT The solution is to **assign something to the variable**. Now the *how* to do that is a bit harder for AudioMixer then the average variable. Only people that worked with Unity will know that. Hence why I tagged your question accordingly. – Christopher Nov 01 '19 at 17:34
  • 1
    Or alternatively, if AudioMixer is a singleton, don't declare a variable here at all, and just use the singleton. I couldn't quickly find a way to _do_ that with AudioMixer, but that's the other approach. – Ian McLaird Nov 01 '19 at 17:35
  • @IanMcLaird is correct. `AudioMixer` is a singleton. There are numerous examples for using this available on the web. Get rid of your `mixer` property and use the `AudioMixer` singleton directly. – 3Dave Nov 01 '19 at 18:54
  • 2
    `AudioMixer` is a design-time class tied to the Audio Mixer window in the editor. It should never be instantiated or dynamically created. – 3Dave Nov 01 '19 at 18:56
  • @3Dave I kinda worried about something that annoyingly complex. I edited your comment into my answer, under full credit. – Christopher Nov 01 '19 at 20:18
  • @Christopher It's actually pretty easy to do in Unity. A substantial chunk of the editor itself is written using Unity, and they expose pretty much everything that you'd need. The engine and some parts of the editor are VERY tightly optimized C++, but everything you'd need, even as an advanced user or coder, is available and typically very well documented. I work with the engine source on a daily basis - I'm constantly impressed, especially vs my experience with UE. – 3Dave Nov 01 '19 at 20:57
  • @3Dave Once I do know the way, it is prett easy. But this is about helping a beginner. For those it is annoyingly complex. You can not even show the solution just with code. | Also I am not a friend of having the Designer be able to do stuff, that I can not do. With the WinForms and WPF/UWP designer, I could at least console myself that it is "just cooking with water too." – Christopher Nov 02 '19 at 00:17
  • @Christopher There is literally nothing that can be done in the editor that cannot be done in code. This is one of Unity's internal requirements. – 3Dave Nov 03 '19 at 00:16