Search Unity

Bug Errors in build game but not in play mode

Discussion in 'Editor & General Support' started by si0ty, Dec 15, 2020.

  1. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    So my project works fine in play mode. When I want to build it I get weird reference errors in the first scene. The game apparently can't find objects which are publicly set in the inspector.

    This is so weird to me. I read about unity having a problem where the scripts are instantiated in a different row when in build mode. Is this a thing or could cause this kind of problem? How should I begin to find the right way of instantiating?
    One thing maybe to mention I'm currently using Mirror for networking. I thought about this maybe mixing things up.

    Unity does have the right "configuration" to build the game in play mode why is everything so broken when exporting? Is this maybe a unity bug?

    Please send me some help... I really fear losing my game because of this.

    Kind Regards
     
  2. Spares

    Spares

    Joined:
    Apr 19, 2019
    Posts:
    3
    I don't have an answer for you as I'm having a similiar problem. I have objects in the inspector which I have put into an array and access the array, however in the build the array is out of order. Your post has helped narrow it down a bit for me.
     
  3. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    What are the "weird reference errors"? Investigate some more until they are no longer "weird".

    Find the fields which don't appear to be working in the build, and add debug.log statements to check what they are, compare it to what you expect.

    You shouldn't be concerned about losing your project as it should be checked into source control. If it isn't, check it into source control.
     
    Last edited: Dec 16, 2020
  4. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    Yeah of course I would normally track them down but as I said a reference error also appears when I set a GameObject publicly in the inspector. This tells me unity is handling this completely wrong. All of the errors shouldn't appear because it is functioning in play mode. In other words, they make absolutely no sense.

    What do you mean by checking it into source control?

    Have a good one
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You are getting an error when you assign something in the inspector? What is the exact error?

    Source control is the thing that removes that fear of losing your game, and enables you to freely hack away at your game until you find the source of the problem.
     
  6. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    this is the error I get in the Player.log:
    Code (CSharp):
    1. UnloadTime: 0.991900 ms
    2. KcpTransport initialized!
    3. UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
    4. UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    5. UnityEngine.Logger:Log(LogType, Object)
    6. UnityEngine.Debug:Log(Object)
    7. kcp2k.KcpTransport:Awake() (at C:\Users\si0ty\Unity\War of Ages\Assets\Mirror\Runtime\Transport\KCP\MirrorTransport\KcpTransport.cs:60)
    8. (Filename: C:/Users/si0ty/Unity/War of Ages/Assets/Mirror/Runtime/Transport/KCP/MirrorTransport/KcpTransport.cs Line: 60)
    9.  
    10. NullReferenceException: Object reference not set to an instance of an object
    11.   at MenuButton.Awake () [0x00001] in C:\Users\si0ty\Unity\War of Ages\Assets\Script\Player\UI\MenuButton.cs:39
    12. UnityEngine.GameObject:SetActive(Boolean)
    13. Intro:Update() (at C:\Users\si0ty\Unity\War of Ages\Assets\Script\Player\PlayerAccount\Intro.cs:93)
    14. (Filename: C:/Users/si0ty/Unity/War of Ages/Assets/Script/Player/UI/MenuButton.cs Line: 39)
    I don't get the error right away when I set it in the inspector I get a null reference error in build version even when it's clearly set and working in play mode.

    I don't know if I understand what you mean. I would love to continue my game it's absolutely bothering me that my fully working game in play mode doesn't work at all when exported...
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Go to your MenuButton.cs line 39 and add some debug.log statements to verify the value of the field you are trying to access and compare between the editor and the build.

    Don't let the error discourage you, having issues in one mode but not the other is not an uncommon problem that everyone runs into at some point.
     
  8. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    As expected Debug.Log is displayed before but not after the set reference. Btw the failed reference I try to get there with GameObject.Find. Maybe is this is somehow causing problems in build?
    I know this way is not perfect but performance doesn't matter in this case.

    I tried to sort this out by setting it manually in the inspector as I mentioned and I STILL get the same null reference error?

    Thanks for the motivation and help in general mate :)
     
  9. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Sounds like there is a good chance that GameObject.Find is the source of the problem. Is the object you are trying to find part of the scene, or are you instantiating it at runtime from somewhere.
     
  10. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    The object is part of the scene. It is instantiated at runtime and doesn't destroy on load.

    Ok, I tried to catch the null reference error there and now I get a null reference error in the AudioManager. There my sounds that are setup in an error in the inspector. He can't find the sound in the array.

    Again this is so weird because he can't get the reference from the inspector which is obviously set

    This is the error I get:


    NullReferenceException: Object reference not set to an instance of an object
    at AudioManager.Play (System.String sound) [0x0004c] in C:\Users\si0ty\Unity\War of Ages\Assets\Script\Player\Audio\AudioManager.cs:41
    at GameManager+<LoadActualScene>d__18.MoveNext () [0x00020] in C:\Users\si0ty\Unity\War of Ages\Assets\GameManager.cs:66
    at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00020] in C:\buildslave\unity\build\Runtime\Export\Scripting\Coroutines.cs:17
    UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) (at C:\buildslave\unity\build\Runtime\Export\Scripting\MonoBehaviour.bindings.cs:91)
    GameManager:LoadScene(Int32) (at C:\Users\si0ty\Unity\War of Ages\Assets\GameManager.cs:60)
    MenuButton:SceneSwitch(Int32) (at C:\Users\si0ty\Unity\War of Ages\Assets\Script\Player\UI\MenuButton.cs:99)
    MenuButton:<Awake>b__16_0() (at C:\Users\si0ty\Unity\War of Ages\Assets\Script\Player\UI\MenuButton.cs:57)
    UnityEngine.Events.InvokableCall:Invoke() (at C:\buildslave\unity\build\Runtime\Export\UnityEvent\UnityEvent.cs:180)
    UnityEngine.Events.UnityEvent:Invoke() (at C:\buildslave\unity\build\Runtime\Export\UnityEvent\UnityEvent\UnityEvent_0.cs:58)
    UnityEngine.UI.Button:press() (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\UI\Core\Button.cs:68)
    UnityEngine.UI.Button:OnPointerClick(PointerEventData) (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\UI\Core\Button.cs:110)
    UnityEngine.EventSystems.ExecuteEvents:Execute(IPointerClickHandler, BaseEventData) (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\ExecuteEvents.cs:50)
    UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1) (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\ExecuteEvents.cs:261)
    UnityEngine.EventSystems.StandaloneInputModule:ReleaseMouse(PointerEventData, GameObject) (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:187)
    UnityEngine.EventSystems.StandaloneInputModule:processMousePress(MouseButtonEventData) (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:642)
    UnityEngine.EventSystems.StandaloneInputModule:processMouseEvent(Int32) (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:548)
    UnityEngine.EventSystems.StandaloneInputModule:processMouseEvent() (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:528)
    UnityEngine.EventSystems.StandaloneInputModule:process() (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\InputModules\StandaloneInputModule.cs:282)
    UnityEngine.EventSystems.EventSystem:Update() (at E:\Unity\2020.1.17f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\EventSystem.cs:376)

    (Filename: C:/Users/si0ty/Unity/War of Ages/Assets/Script/Player/Audio/AudioManager.cs Line: 41)
     
  11. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Is it being instantiated within the Awake() function of another script?
     
  12. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    So the Player GameObject is instantiated when the game starts idk if this is already considered runtime. The menu button sets the reference in its own awake function.
     
  13. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Find the line of code that instantiates the player. Is it in an Awake() function, or a Start() function?
     
  14. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    it's in Awake
     
  15. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    In order for your button to find the player, the player needs to first exist.

    Add a Debug.Log statement to where you instantiate the player. I think you will find that in the editor, it is creating the player and then the button is finding it. But in the build, the button is trying to find the player before it is created.
     
  16. si0ty

    si0ty

    Joined:
    Jun 28, 2019
    Posts:
    24
    The button is instantiated later in the scene so the player is clearly instantiated then.

    And this makes no sense when I look at the error at the AudioManager where the Sound is set manually in the Array. Here again: the audio manager plays the first song a couple of seconds into the game after a camera pan. In both cases, the player should be there.

    I even have an error message when the Sound "s" is null. it doesn't trigger but then it says null reference error?!

    Here that's the function where the error from above appears in line 41. It's line 8 here:

    Code (CSharp):
    1.     public void Play(string sound) {
    2.         Sound s = Array.Find(sounds, item => item.name == sound);
    3.         if (s == null) {
    4.             Debug.LogWarning("Sound: " + name + " not found!");
    5.             return;
    6.         }
    7.  
    8.         s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
    9.         s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
    10.  
    11.         s.source.Play();
    12.     }
    This makes no sense am I right?
     
    Last edited: Dec 17, 2020