How to call a public function automatically when I return to the Main Menu from any levels?
Hi guys,
so I have a menu scene where I can switch between 2 pages of that menu. First page of the menu is some info about the game and exit and the second page of the menu is summary of levels.
Ok, now I want to start the game. First page of the menu shows up. Then I want to choose a level to play so I go to the second page of the menu. In there I click on any level. The level loades and I play. But when I get killed I want to return to the second page of the menu.
I switche between menu pages using buttons and public functions like: "public void ShowPageOfLevels" "public void MenuFirstPage". I have buttons using OnClik() that triggers this functions. But how to call the function when I return from any other scene?? Or in general how to return to the second page of the menu??
You could do something like this in your update function:
bool tick;
string current = "Start $$anonymous$$enu";
void Update() {
if (current == "$$anonymous$$ain $$anonymous$$enu" && !tick) {
tick = true;
//Call code here:
}
}
You just need anything that can identify which menu is currently visible, it doesn't have to be a string.
I do not understand. I am not using Update. In my menu scene I only have UI$$anonymous$$anager with a scipt attached with some public functions that I call when I click on a button in the menu to switche between menu pages. Level is a different scene. When the level ends, I want to return to the menu. Now, I am using Application.LoadLevel("$$anonymous$$ain$$anonymous$$enu"); But what I get is the first page of the menu. The second page of the menu does show up when I call a function from UI$$anonymous$$anager "public void ShowPageOfLevels". I want to return from any level to the second page of the menu :-/
Answer by xxluky · Nov 03, 2015 at 02:59 PM
I finaly figured out a different way how to solve my problem. I created a private static bool. Static variables are instances of the class and are not reseted when loading a different scene.
My script now looks something like:
private static bool isInMenuMoreThanOnce;
void Start()
{
if(isInMenuMoreThanOnce)
ShowPageOfLevels (); // Calls the function that displays the page with levels
}
public void ShowPageOfLevels ()
{
if (!isInMenuMoreThanOnce)
isInMenuMoreThanOnce = true;
// some other code of the function
}
Thanks guys for your help.
I'm pretty sure that book should be non-static, because you would then need an instance of the object to receive it.
Answer by Fire_Cube · Nov 02, 2015 at 07:25 PM
if the menu is in two scenes, just load directly the second one. If it's in one scene, and you need to call the function to go to level page, just create a gameobject with a certain script on it, and call DontDestroyOnLoad with the object in parameter.
there is the script i mentioned before :
using UnityEngine;
public class loadWithLevelPage : MonoBehavior
{
void OnLevelWasLoaded()
{
Gameobject.Find("theObjectWithTheMenuScript").GetComponent<TheMenuScript>().ShowPageOfLevels();
Gameobject.Destroy(gameobject);
}
}
Answer by MakakWasTaken · Nov 02, 2015 at 05:14 PM
You can then use OnLevelWasLoaded like this:
void OnLevelWasLoaded(int level) {
if (level == 0) { //Assuming that the Main scene is the first scene under the list in File->Build Settings
//Call the function here:
}
}
Yeah, that would work but when I start the game I want to show the first page of the menu. I want to display the second page of the menu only when I return to the menu from gameplay where I am using Application.LoadLevel("$$anonymous$$ain$$anonymous$$enu");
Then you can do something like the this:
bool check;
void OnLevelWasLoaded(int level) {
if (!check) {
check = true;
return;
}
if (level == 0) { //Assu$$anonymous$$g that the $$anonymous$$ain scene is the first scene under the list in File->Build Settings
//Call the function here:
}
}
It is strange. Your code does not work. But when I use this:
void OnLevelWasLoaded(int level)
{
ShowPageOfLevels ();
}
it is working. The function OnLevelWasLoaded is not called when I start the game but it is called when I load it again. Isn't it strange? Isn't is only when using the editor?
The function should only be called when calling Application.LoadLevel(); I am not sure if it is called when starting the game. The function is also called in builds.
Ok, I am not sure about this function. It should be called when I start the game but it is not. It is called only when I use Application.LoadLevel. Can anyone thing of something else please?
That is the expected behaviour of the function, but I thought that was what you wanted? If not what exactly do you want the function to do?
Yes, it is working. But I thing it has a doubtful behavior, or doesn't? I want to call the function not on the very first time but only when I return from the gameplay (when I enter the menu second time). It Is working just like that. But I always thought that this function is being called when level is loaded. In this case I thought that this function is always called even when I start the game because it is in level of index 0.
So my question is: If I have a script in the scene of index 0 (File->Build Settings) using function OnLevelWasLoaded is this function called when I hit play? In my case it IS NOT but I always thought IT IS! ??