Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xxluky · Nov 02, 2015 at 04:36 PM · menufunctionsload scene

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??

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MakakWasTaken · Nov 02, 2015 at 04:43 PM 0
Share

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.

avatar image xxluky · Nov 02, 2015 at 05:10 PM 0
Share

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 :-/

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MakakWasTaken · Nov 03, 2015 at 03:02 PM 0
Share

I'm pretty sure that book should be non-static, because you would then need an instance of the object to receive it.

avatar image
1

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);
 }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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:
 
     }
 }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxluky · Nov 02, 2015 at 05:21 PM 0
Share

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");

avatar image MakakWasTaken · Nov 02, 2015 at 05:23 PM 0
Share

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:
  
      }
  }

avatar image xxluky · Nov 02, 2015 at 05:48 PM 0
Share

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?

avatar image MakakWasTaken xxluky · Nov 02, 2015 at 05:52 PM 0
Share

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.

avatar image xxluky · Nov 02, 2015 at 05:57 PM 0
Share

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?

avatar image MakakWasTaken xxluky · Nov 02, 2015 at 06:00 PM 0
Share

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?

avatar image xxluky · Nov 02, 2015 at 06:15 PM 0
Share

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! ??

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to call a block from a fungus flowchart in the different scene? 1 Answer

I have a problem with the pop-up lose menu and win menu Please help!! 0 Answers

How to keep overlay menu as I load different scenes. 0 Answers

2 doubts button and script 0 Answers

Control animator animation with UI Slider 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges