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

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

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

Best Practice For Player Stats Data 1 Answer

PlayerScore prefab in unity gives value in gameover screen as 0, how do I show the final value in GameOver scene? 0 Answers

Score counter not working properly. 1 Answer

when i load scene 2, score adds automatically x6 (Weird bug or something) 2 Answers


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