Functions on GUI elements disapearing after reloading the scene
So, I've got a little game, and right now I have a hand full of scenes, most of them are levels, but, I do have a Level Selector screen (Serving as a Start Menu for now ), and for some reason, when I leave the level selector scene and then come back to it, doesn't matter the level I was in, the functions that were assigned to each GUI element, in this case, panels, disappear, which makes it impossible to get out of the Level Selector.
Images for Context:
//Level Selector Working
As you can see, everything is working, and in the bottom right corner, you can see that the function OnClick is assigned to the Level Manager object. (There's no level name in there because I have all the panels selected )
//Level Selector Not Working
As you can see, after I reload the Level Selector scene ( Doesn't matter the way I reload it, it happens regardless of the level I was in before I went to the level selector window ) it loses the objects that it had in the first load, even though the objects are still there as you can see in the Object Window.
In case that the code from the Level Manager object is helpful, here it is.
//Level Manager Code
Thanks for the attention and kind regards!
Answer by AdmiralThrawn · Aug 06, 2017 at 11:39 AM
Hi @demong70,
It's hard to see the details of your inspector in the screenshots you've provided, but I guess it's a button component.
I haven't been "there" yet to reload a scene and keep onClick functions, but can you add a listener programmatically when you load the scene? Then just save and load any values you have had in the previous scene and use them in a code like this:
int someInt = 0; // load from previous scene..
Button button = gameObject.GetComponent<Button>(); // this or any gameObject you need
button.onClick.AddListener(delegate ()
{
// use your saved variables to assign them to method calls..
SomeFunctionToCall(someInt, 1f);
});
Let me know if you found a practiable solution.
Cheers, AT