- Home /
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
Assets/-Script/InterfacciaUI/ImpostazioniUI.cs(201,31): error CS0103: The name `PlayerSettings' does not exist in the current context
Error building Player because scripts had compiler errors
Build completed with a result of 'Failed' UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)
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?
Answer by jackmw94 · Nov 16, 2020 at 01:19 PM
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! :)
hi jack, thank you so much for the reply!
I've tried and now it works!
I wanted to use PlayerSetting settings as an option that the user could disable / enable but in the final build I can't use it so...
$$anonymous$$y goal was to create a setting that turns off the app sound when you return to the home screen of the device (or just keep the game in the background).
I haven't checked if there are any features that can make the game understand when you keep it in the background, I hope so.
I will try to search if there is a simpler and more effective solution, thanks again for the detailed answer! :D
Your answer
Follow this Question
Related Questions
CommandInvokationFailure: Gradle build failed 2020 0 Answers
Unity Folders -1 Answers
i get an error when the host shots the client in Unet 0 Answers
Cant build my Game! UnityEditor Namespace not found? 2 Answers
Unity 2017 is not working. 2 Answers