Little problem with PlayerSetting Editor Error...

Hi, today I’m here to talk about a problem with my script (I don’t speak English well I’m using google translate, I hope you understand what I write …)

I’ll explain the problem that I currently have and that I can’t solve (my knowledge in C ++ is low, what little I know I learned from video tutorials).

I am creating a game for android and at the moment I have created a menu with various settings (graphic, audio, display etc etc).

I’ve for a week now, been trying to figure out how to use PlayerSettings.muteOtherAudioSources = true

The script works correctly when I run the game in the PC but when I compile the APK the unity console gives me these 4 errors


  1. Assets/-Script/InterfacciaUI/ImpostazioniUI.cs(201,31): error CS0103: The name `PlayerSettings’ does not exist in the current context
  2. Error building Player because scripts had compiler errors
  3. Build completed with a result of ‘Failed’
    UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)
  4. UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors
    at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (BuildPlayerOptions options) [0x00242] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:194
    at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x0007f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:97
    UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)

Inside the ImpostazioniUI script I entered using UnityEditor;
The error line of code is simply this

if(mutaaudiofuorigioco==1) {PlayerSettings.muteOtherAudioSources=true;}

Looking for solutions on the internet I have tried various solutions and it seems that I will have to use another script via the Editor folder.

But now I have another problem, I would like to access the script functions inside the Editor folder, what can I do?
Or if there is another way, how can I get PlayerSettings.muteOtherAudioSources to work on android;
Is it possible or should I let it go?

The good news is that this is only 1 error.

PlayerSettings is part of the UnityEditor library, which means you can’t use it or modify it within build code. PlayerSettings is a configuration so you should just be able to go Edit-> Project Settings then click the ‘Player’ option, click the tab for the platform you’re building for and check mute other audio sources there. Then save the project. Now you shouldn’t need to alter that value in code and removing it will mean your project can build.

Alternatively, if you HAVE to modify it in code (e.g. if it’s within a build script), then you’ll have to wrap any calls to PlayerSettings inside #if UNITY_EDITOR defines.

#if UNITY_EDITOR
    if(mutaaudiofuorigioco==1) {PlayerSettings.muteOtherAudioSources=true;}
#endif

and to prevent the UnityEditor library being included in your build, wrap the ‘using’ directive in editor only defines too at the top of the ImpostazioniUI script:

#if UNITY_EDITOR
using UnityEditor;
#endif

But know that this will then ONLY be called within the editor before building and NOT on the build itself. If you’re adding this as a setting the user can change then I don’t think this will work.

Hope this has helped, let me know if you need any more information! :slight_smile: