• 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 importguru88 · Jun 15, 2016 at 06:19 AM · c#scenescoresingletonpattern

How do switch scene with singleton pattern unity3d C#

I have to two UI text . One timer script and one score script .The scenes won't switch when the countdown timer reaches zero. Will dont destroy on load work if I a call that function in the score script change the scene ? I have points set to 50 . The scene will only change if the score is below 50 and the timer reach zero. Here are my script.

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Game : MonoBehaviour {

private MyClockScript myClock; void Start () { myClock = GetComponent(); }

 // Update is called once per frame
 void Update () {
  if (myClock.m_leftTime < 0 || GameManagerSingleton.score > 0)
        {
            SceneManager.LoadScene("ui");
        }
 }

}

public class GameManagerSingleton : MonoBehaviour { public static GameManagerSingleton instance = null;

  public static int score;
  


  private Text text;
  
  void Awake()
  {
      text = GetComponent <Text> ();
      score = 0;
      if (instance != null && instance != this)
          Destroy(gameObject);    // Ensures that there aren't multiple Singletons
 
      instance = this;
  }
  
  void Update()
  {
     text.text = "Score: " + score;
      Debug.Log("Score: " + score);
  }
 
 

}

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

2 Replies

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

Answer by YoungDeveloper · Jun 15, 2016 at 11:00 AM

Best solution that works best for me personally is never again load the scene with singletons. Create fake preload scene (0) with all the singletons where they inizialize. If you want to reload the game or return to lobby, then load scene (1). And dont forget to call DontDestroyOnLoad() on singleton gameobjects.

Comment
Add comment · Show 2 · 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 importguru88 · Jun 15, 2016 at 08:11 PM 0
Share

What do you mean by preload scene . Where at on the singleton script would I call the DonDestroyOnLoad function ?

avatar image YoungDeveloper importguru88 · Jun 16, 2016 at 11:59 AM 0
Share

By preload scene i basically mean a basic scene, like any other, which does not serve the game itself any purpose, except for initalizing singletons. Calling dont destroy on gameobject ensures that gameobject wont be destroyed when scenes change.

 void Awake() {
         DontDestroyOnLoad(gameObject);
 }
avatar image
0

Answer by mr-gelmir · Jun 15, 2016 at 10:53 AM

Could you make sure the scene called "ui" really exists and is added to the Build settings? Unity can only load scenes that are included there.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Best Practice For Player Stats Data 1 Answer

Can I do two Singleton Pattern Design scripts in my scene Unity C# 1 Answer

how to disable dontdestroyonload on a specific scene 2 Answers

Best Score Counter 0 Answers

UNET Multiple online scenes 0 Answers

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