I have some levels and a main menu. When starting the game from the main menu (in the editor debug mode) I can select a level a load it, no problem. However, when I wish to go back to the main menu using


A whole lot of my gameobjects throw exceptions like

The gameobjects is destroyed, but you are trying to access it


Missing reference exception

and so on.

However, when i start the level from the editor (and not using the main menu), I can go back to the main menu, it works as it should.

NOTE that I don't have any persistent gameobjects. I do have singletons, but no persistence here. Any ideas what could cause the issue?

  • \$\begingroup\$ The singletons sound like probable suspects, but you haven't shown us enough of your code to make a positive diagnosis. Try stripping your scenes down to the bare minimum required to generate one error, then post the code where that error is thrown (identifying the line) and the code for the thing it's trying & failing to reference. \$\endgroup\$ – DMGregory Aug 3 '17 at 20:06
  • \$\begingroup\$ Thanks for the input. Well I would like to show some code, but I have no clue what might cause the issue and I have a couple of thousands of lines of code in place, and i can't really expect anyone to chew through all of it. I think I redo the functionality fare and square and hope for the best. \$\endgroup\$ – agiro Aug 4 '17 at 7:06
  • \$\begingroup\$ That's why we recommend creating what's called a Minimal, Complete, Verifiable example. You can do this by copying your project and deleting stuff until the problem disappears, or starting a new project and copying your old code into it until the problem appears. Attacking it from either direction lets you narrow it down to just the bits of code that are required to reproduce the issue. \$\endgroup\$ – DMGregory Aug 4 '17 at 12:21
  • \$\begingroup\$ I'm rather reworking the whole structure of the menu logic. See, I had fuctionality on a lot of objects, data on a lot of object, save on a persistent object that I removed later, then newer objects appeared that use logic from this and that object with data from that and that etc etc so it was a mess. Now I have one persistent w/ load/save, shareable data and functionality (like class libs, general use). Other GOs just notify them they need something and all the initialization is in one place, on the persistent object. \$\endgroup\$ – agiro Aug 4 '17 at 12:35
  • \$\begingroup\$ If that solved your problem, then I'd recommend writing it up as an answer. It's easier for other users experiencing similar issues to find it that way, and you can go into more detail than a comment allows. Plus, you can earn upvotes from other users if they find yous solution useful. :) \$\endgroup\$ – DMGregory Aug 4 '17 at 12:46

Use DontDestroyOnLoad. It ensures that your Menu items will live beyond the point at which you load a new scene. This may allow you to work around the problem, as you probably always want the menu openable from within the game.

Better yet: Make one scene, and keep everything in there. But that can be slow to work with, depends.

  • \$\begingroup\$ Thanks for the answer. Well one scene is not really an option as I have many levels, which include a lot of sprites and meshes, all together would take up around 1 GB of RAM which is unacceptable in a mobile game of the caliber I make. But yes I think I will keep most of main menu's functionality intact throughout the game, it shouldn't make too much of an impact. \$\endgroup\$ – agiro Aug 4 '17 at 7:08
  • \$\begingroup\$ By the way, how about caching all my gameobjects using Unity's JSON before turning anything off? Then use that as an object pool, loaded as soon as main menu is active? I mean I would have a list or linked list of gameobjects. \$\endgroup\$ – agiro Aug 4 '17 at 7:12
  • \$\begingroup\$ @agiro Glad I could help. You've asked one question; I've answered that question. I'm not speculating on other things, now. If this helped you to solve your problem, please accept it (as that costs you nothing and shows the community you mean well) and then by all means, ask a new question. We do not encourage reams of further questions in comments, following the first question being answered; especially as you are here proposing something entirely different that wasn't mentioned in the initial question as posed. Thanks for understanding, it's just site etiquette. \$\endgroup\$ – Engineer Aug 4 '17 at 8:00
  • 1
    \$\begingroup\$ True that one. btw it's strange that I can use stackoverflow the right way but mess up using this site out of despair :D I usually ask questions here when I have no idea how to make things work. \$\endgroup\$ – agiro Aug 4 '17 at 8:08
  • 1
    \$\begingroup\$ @agiro Haha. Not to worry. Thanks for the accept and best of luck. \$\endgroup\$ – Engineer Aug 4 '17 at 8:08

With some days of reworking the whole Main Menu functionality from the ground up, I think I finally got it to work.

The issue was that I didn't structure my stuff as I should, namely I had logic on multiple objects, data on multiple objects and everything was dependent on everything. Without structure, I basically wrote logic to any script that was open, needless to say it was kind of dumb. But the main reason this crashed my game is that even if my objects are by default turned on (active), somehow after loading the main menu they remained turned off.

Now I have a persistent gameObject named GameData with a script on it named Persistence. It uses DontDestroyOnLoad and is a singleton, this is responsible for actually calling the initializers on the gameobjects.

At the first - as this main menu is sort of a small scene - I get all scene gameobjects into a collection - and only query them later on using LINQ. Also, I have no Awake or Start only any gameobjects in the scene.

Now, I also made an abstract class named InitializeMe that looks like so:

public abstract class InitializeMe : MonoBehaviour
    public int orderNumber;
    public abstract void INIT(Persistence pers);

Where the ordernumber is used in a LINQ query when getting the menu's gameobjects who has an InitializeMe on them.

Then I have descendants of this InitializeMe that obviously implement that abstract class and tell exactly what to do in their case. As They get access to Persistence easily (they are called from that very class) and this Persistence holds most of the data that is ever needed, initialization is made very easy in an order easy to predict and control.

With this and the save/load and check file integrity coming first, loading a scene was rather quick and stable.


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.