2

I have a game with three scenes, two level scenes and a menu scene. When I press play on my level scene the first time, everything works perfectly, but when I go to the menu scene and then later return to the level scene,

the references on the scripts attached to the NetworkManager GameObject were reset. On the scripts shown in the picture below, for example, all but the references that were linked to prefabs in my assets were reset.

I've tried assigning each and every variable with code instead of doing it manually, but even that doesn't work.

This GameObject does have dontdestroyonload on. Why are these references getting reset specifically on this object?

Fattie
  • 27,874
  • 70
  • 431
  • 719
Aaron Ge
  • 258
  • 3
  • 21
  • Everything you are doing is incredibly wrong :) Fortunately the solution is absolutely simple: http://stackoverflow.com/a/35891919/294884 – Fattie Jul 30 '16 at 02:24
  • @JoeBlow do you know of any tutorials that will teach me how to make the transition? I am unsure of how I can put all of my general scripts, sound effects, etc on a preload scene. Is this preload scene a necessity? Can I just create a scene with a loading animation in between transitions? – Aaron Ge Jul 30 '16 at 02:36
  • @JoeBlow I already have over a hundred scripts attached to this game, and it's getting kind of complicated. I can't believe I went this far without knowing about this. – Aaron Ge Jul 30 '16 at 02:44
  • Hey @aaronge - right, it's totally normal in Unity that experienced programmers like yourself start using Unity and because Unity has zero documentation, and a couple of incredibly silly things in it, issues like this happen! I mean it's madness to have a game engine without the concept of a pre-load scene "built-in", you know? Total madness. When they finally get around to adding in that concept, it will really incredibly slash like 50% of the QA problems about Unity, heh. Unity's like that - for years they had this utterly hideous UI system - but now an incredibly good one ("at last"). – Fattie Jul 30 '16 at 13:16
  • Thank you @JoeBlow . I recently added a loading scene, but it doesn't function properly. Would an experienced programmer such as yourself mind taking a look at it? – Aaron Ge Jul 30 '16 at 14:45
  • @JoeBlow I posted a new question 13 hours ago. Sorry, I forgot to mention that. – Aaron Ge Jul 30 '16 at 17:45
  • @JoeBlow http://stackoverflow.com/questions/38670275/preload-scene-stops-responding-upon-transitioning-unity-3d – Aaron Ge Jul 30 '16 at 17:51

2 Answers2

1

I don't have enough rep to comment, but did you save your scene after you added the references to the objects?

Answer (By Aaron Ge): I unchecked Don't Destroy On Load and Run In Background, and then my issue was fixed. I am unsure of how this fixed my issue, but it did.

Possible Explanation: http://answers.unity3d.com/questions/734445/scripts-in-other-scenes-still-executing.html

If you use DontDestroyOnLoad on objects that are placed in a scene, make sure that you either:

Load that scene only once. That's usually called a loading scene. The scene just contains certain manager objects and it immediately switches to the menu scene / whatever... You would never load that first scene again.

If you want to load the scene again and you used DontDestroyOnLoad on one or more object, you have to destroy the objects manually. You can't prevent the objects from being created again since they are part of the scene. Usually al objects get destroyed automatically when you load a new scene, DontDestroyOnLoad prevents that.

Almond
  • 168
  • 1
  • 8
  • Yes, its not that. @Almond – Aaron Ge Jul 29 '16 at 23:14
  • Does the same issue occur if you start by playing the menu scene first rather than starting the level and coming back to it? – Almond Jul 29 '16 at 23:16
  • No, my menu scene isn't affected by this at all. Only this specific game object was affected by this issue in my level scene. @Almond – Aaron Ge Jul 29 '16 at 23:20
  • Allow me to clarify. If you start the menu scene, and then go to your level scene, are the objects missing the references? (Its been a while since I've worked in Unity, but unless the GameObjects are statically part of your scene, you'd have to dynamically create them and then assign them to your script references onstart().) – Almond Jul 29 '16 at 23:23
  • No, I am able to start on my menu scene and then go to my level scene with all of the references Intact @Almond – Aaron Ge Jul 29 '16 at 23:25
  • Not to insult your intelligence, but are you loading the correct scene? Or is this an issue with both level scenes? – Almond Jul 29 '16 at 23:28
  • Yes I am loading the correct scene, and this is an issue with both level scenes. – Aaron Ge Jul 29 '16 at 23:30
  • I found a fix to my issue. I unchecked dontdestroyonload and runinbackground and the issue stopped. I do not even know how it happened, but it did! – Aaron Ge Jul 29 '16 at 23:32
  • Check my edited answer. Could add some explanation to your solution. – Almond Jul 29 '16 at 23:43
0

I unchecked Don't Destroy On Load and Run In Background, and then my issue was fixed. I am unsure of how this fixed my issue, but it did!

Aaron Ge
  • 258
  • 3
  • 21