1
\$\begingroup\$

I've been using this small script as a component to any object I want to not be destroyed at scene loading.

public class DontDestroy : MonoBehaviour
{
    void Start()
    {
        DontDestroyOnLoad(gameObject);
    }
}

It has been working fine. It works for my GameOver canvas which I've been using for a long time whilst making the game.

Today I added the same script to a new AndroidControls Canvas I made. In the Editor it worked fine. Then I built it for Android to run on my physical phone (Galaxy S10). It gave me a Null exception.

It turned out to be caused by the AndroidControls Canvas being destroyed between scene loading (even though as I say, I tested in Editor for a few runs and it didnt get destroyed). And it gets a little weirder because after the attempted build to APK, and fail on physical device, it then failed to work in the editor. It continues to not work in each test after this in Editor, however if I remove the component and add it again, the entire process then repeats.

Please help as I don't have a clue how to start fixing this. The only thing I can imagine it being wrong is it started as soon as I used this script on two canvas's simultaneously. (I highly doubt I am correct but it's the only thing I could notice at all that might be causing this).

This picture actually shows my DontDestroy component is attached to Canvas_AndroidControls, but in the Hierarchy you can already see that it is not in the DontDestroyOnLoad section (excuse the pink overlay for play-mode):

enter image description here

Maybe relevant, here are some of my specs:

Win 10
Unity 2020.3.5f1 (Just downloaded NDK/SDK for Android today via Unity Modules)
Android 11 (Samsung Galaxy S10)
\$\endgroup\$
4
  • \$\begingroup\$ Unity 2020.3.5f1 is missing from the Download Archive and Release Notes. I'm not sure if that was a mistake on Unity's part, or if they removed it due to some kind of major issue with the build. Regardless, perhaps you should start by updating to 2020.3.9f1 \$\endgroup\$ – Kevin May 25 at 1:46
  • \$\begingroup\$ Ah ok. Hopefully doing that doesn't cause me any new issues :) btw, I 'fixed' the problem by adding this line into another script (my GameManager script was also set as DontDestroy so in its Start() I did: DontDestroyOnLoad(AndroidCanvas.gameObject) and so far everything is working. I am gonna update now though as its not so good if something that should work does not. Strange because I chose 3.5f1 specifically because it said it was the LTS version at the time when I got Unity Hub. \$\endgroup\$ – I_Keep_Trying May 25 at 12:16
  • \$\begingroup\$ Well updating the editor has messed up the project. I cannot use it currently. It doesn't recognise the UnityEngine API such as Monobehvaiour etc in VS2019 any more..... Arghhhh \$\endgroup\$ – I_Keep_Trying May 25 at 13:20
  • 1
    \$\begingroup\$ Well, two things: Updating the Editor to 2020.3.9f1 DOES solve the original problem (as far as I can tell after a couple of quick builds). The annoying problem of VS2019 not recognising Unity library I fixed in Unity>Edit>Preferences>External Tools>Click Regenerate Project Files button. Now I take the 'LTS' label Unity add with a pinch of salt \$\endgroup\$ – I_Keep_Trying May 25 at 13:27
1
\$\begingroup\$

EDIT: Since writing this answer, the problem has reappeared. Extremely odd how it worked for a few builds. Then all I did was change the music from one file to another, build again and suddenly the exact same problem process has resurfaced.

(If somebody did wish to see the project, pending me adding it to Github. I zipped it in it's current state, 2 part download: https://file.io/j9ofNLrj4Pp6 , https://file.io/tNkot6Yd6gPx) The problem is evident as soon as you open the project in Unity.

As I stated in the comments above. I can fix the problem by ditching my 'DontDestroy' component completely and just adding the lines to not destroy each object in my GameManager.cs Start() instead.

Seems Unity 2020.3.5f1 LTS is a version of Unity to be avoided. There was no Error or Warning regarding this issue and I had to mess about a while debugging to even find why the original Null Error was thrown.

Until now I was under the impression that Unity builds with LTS tag where very stable and tested builds: https://unity3d.com/unity/qa/lts-releases?version=2020.3

I can confirm that installing 2020.3.9f1 LTS fixes the issue. I removed my workaround code line (as mentioned in comments on question), and re-added my original 'DontDestroy' component to the objects. Now they all remain and are never destroyed upon Scene change.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ The LTS versions are versions which don't have any of the newer features or major changes which might break something. But they still receive regular bugfix releases. Installing those is usually a good idea (as long as you have your whole project under version control, so you can revert in the unlikely case that it breaks nevertheless). \$\endgroup\$ – Philipp May 25 at 13:41
  • \$\begingroup\$ Thanks Phillip. Definitely going to invest the tiny bit of time required to set up VC. Would be a massive shame to lose all this work. Nearly regretted my 'seat of the pants' approach to programming then :P \$\endgroup\$ – I_Keep_Trying May 25 at 13:47
  • \$\begingroup\$ Definitely! You should put every project under version control as soon as you have something you would miss if you accidentally break it. It does not just protect you from software errors. It also protects you from yourself. For example, when you try to improve something in your game but end up making it worse, but you don't remember how your code looked before you did it. \$\endgroup\$ – Philipp May 25 at 14:08
  • \$\begingroup\$ I used to use github extensively with my projects. I defintely see the huge value to it but for some reason after jumping back into coding I just thought of it has a needless complication. You're right though now is the time to get that sorted!! However, sadly my answer here is wrong. The problem suddenly started again and the same 'Canvas_AndroidControls' object is no longer affected by my 'DontDestroy' component (ie. Its now being destroyed when it should not be, even in new Unity build) \$\endgroup\$ – I_Keep_Trying May 25 at 15:08
  • 1
    \$\begingroup\$ LTS releases are designed to be stable and only include bug fixes, but they can and do sometimes accidentally introduce new bugs from version to version. Some developers prefer to find a version that seems stable to them and stick with it. I tend to prefer upgrading to the latest version each time Unity releases an update \$\endgroup\$ – Kevin May 25 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.