• 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 LoKo6264 · May 21, 2016 at 09:29 PM · main menuendback

Back to main menu after 3s from show ui text

Hi, I made game with collectibles and after collect all of them on the screen we can see "you won" which is UI Text. I'd like to back to main menu after 3 seconds from this text. This UI Text is including all game scene. In my build settings main menu has number 2 - for easier make code for me. How then can I do it, with script I guess?

Comment
Add comment
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

1 Reply

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

Answer by corn · May 22, 2016 at 07:15 AM

Easiest way to do it would be to attach a script to your text, and only activate your text's GameObject when you show it, so that you can call the code to load the scene with the OnEnable callback. To wait for 3s, you need to use a Coroutine.

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 public class LoadScene : MonoBehaviour
 {
     [SerializeField]
     private int m_SceneIndex = 2;
 
     [SerializeField]
     private float m_DelayBeforeLoad = 3f;
 
     private void OnEnable()
     {
         StartCoroutine(WaitAndLoadScene(m_SceneIndex, m_DelayBeforeLoad));
     }
 
     private IEnumerator WaitAndLoadScene(int sceneIndex, float delay)
     {
         yield return new WaitForSeconds(delay);
         SceneManager.LoadScene(sceneIndex);
     }
 }

However, it would be better to write a public function that you would call right after showing the text, in which case you'd need to replace private void OnEnable() by public void Load().

Comment
Add comment · Show 3 · 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 LoKo6264 · May 22, 2016 at 11:24 AM 0
Share

I copied and pasted this script but game changes to scene 2 immediatly after 3 seconds ins$$anonymous$$d after this ui text show up, any ideas?

After replace private void OnEnable()to public void LoadScene()I get error "'LoadScene': member names cannot be the same as their enclosing type".

avatar image corn LoKo6264 · May 23, 2016 at 05:51 AM 0
Share

If you use OnEnable, you need to attach the script to the GameObject to which the Text is attached, and disable it. You have to enable it only when the game is over.

As for LoadScene(), my bad, you cannot have a public function with the same name as the class, unless it's a constructor. Just call it Load() or whatever.

avatar image LoKo6264 corn · May 23, 2016 at 12:48 PM 0
Share

I made it on my own with different way, exactly ins$$anonymous$$d after "you won" game takes you to main menu after 3 seconds I made 2 buttons for go to main menu and exit from game. But of course thank you for help , you got Accept :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Rigidbody is being dragged back to bouncy object 0 Answers

Accessing my 'Options' panel from another script. 1 Answer

Freeze on stop. Freeze when deleting GameObjects during runtime.,Slow to delete. Slow to exit play mode. 1 Answer

I want to know if should I make my main menu for my video game first or last? 0 Answers

Rotate Ball specific number of degrees and back again 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges