- Home /
UI Panel Don't Destroy On Load?
Is it possible say to have a timer on a slide out UI panel not destroy when a scene changes? For instance if I have a trip timer and I set the timer, then slide the panel with the timer on it out of view, then I go to another scene and then come back to that scene have the trip timer continue keeping accurate time? Hopefully I explained that right?
I'm still having trouble with this one, it seems I can get a regular GUI text timer to work on an empty game object. Now the odd thing is that if I make that game object a child of my canvas it is not visible in other scenes (Which is what I want) but if it is not a child of the canvas then it IS visible in other scenes (Which is what I don't want)
As for the other timer that is on my animated slide in panel and uses UI text, that one will ONLY play in my main scene regardless of how many variations of the Don't destroy on load I try. Can this even be done with the new UI system?
I hadn't noticed this before now bt when I exit my scene and then come back in that my timer game object is being duplicated?
Answer by DiegoSLTS · Mar 09, 2015 at 01:38 AM
You say you tried "a few variations" but it sounds you only tried setting the Panel to not be destroyed. I think the "DontDestroyOnLoad" only works with game objects at the top level of the hierarchy, if you set your Panel to not be destroyed but you let the Canvas containing it be destroyed, the Panel will be gone too.
Try calling the DontDestroyOnLoad for the Canvas or whatever is at the top level that contains the panel, every child of that object should still be there when you change the Scene.
Also, Neamtzu talked about making it a Singleton, but a singleton and something that is not destoyed when switching scenes are not the same thing. A singleton gameobject needs the DontDestroyOnLoad method, but you can keep anything from one scene to another even if they're not intended to be singletons.
I did try using the don't destroy on load on the canvas containing the panel. I made a new canvas, set it to top level and then put the don't destroy on that. $$anonymous$$y panel I then made a child of that canvas. It did carry my timer to other levels but when I return to the main level. Original level of the very fist timer it no longer functions.? This is very confusing and makes no sense to me as to why it would act that way.
I have also tried the same idea with my music player that has all sorts of elements attached to it like a music visualizer and text based spectrum data display and the same thing occurs with that when I return to it's origin level. the music still plays but not of the controlling buttons work any more and the music visualizer no longer works along with the text based spectrum data display. And yet all of these things are children of the canvas that has the don't destroy script on it???
Remember that the only objects that will remain after the load level are the ones marked with DontDetroyOnLoad and their children, if your presistent object has references to other objects that are destroyed, those reference will be nulled, and if you return to that level the references will remain null, they're not automatically updated.
Yup, that's pretty much what is happening, now is there a way to fix that?
I can think of a few methods:
$$anonymous$$ark every dependency to not be destroyed on level load too
Use the OnLevelWasLoaded method to search new objects in the loaded scene that can replace the destroyed ones
Similar to the second method, but ins$$anonymous$$d of the OnLevelWasLoaded put an object in the scene with a Start or Awake method that replaces the destroyed references.
But I've never done this, keeping objects between scenes works better to pass some data, when you have a lot of references it gets a lot harderd to make it right.
Also, consider maybe using LoadLevelAdditive to load the new level. Nothing will be destroyed, so you'll have to manually clean the scene, but it might be easier in some cases.
Answer by Jabeebus · Jun 24, 2016 at 09:19 AM
To stop the panel from being destroyed, write this
void Awake ()
{
DontDestroyOnLoad(this.gameObject);
}
and attach it to the Canvas of the panel because UI's can only be visible if their in a Canvas parent.
The parent and the children won't be destroyed when you load something
Your answer
Follow this Question
Related Questions
Increase as timer decreases 1 Answer
What is a good method for tracking an array of different times? 2 Answers
Is it possible to make a timer that ignores timescale? 2 Answers
Will DontdestroyonLoad() Keep Child Objects too 2 Answers
Are there any performance differences between a timer and using Invoke? 1 Answer