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
1
Question by Isaacg · Jan 11, 2017 at 12:44 PM · bugloadlevelstartsingletonawake

Start is not called after reloading the level

Hi, I am making a class which it is supposed to load every time I load a level, but the methods Start and Awake is only loaded the first time this level is loaded, and as a result, the number of lives is not displayed when the level is restarted. Here is the important part of my class:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 //TODO: Cuando se recarga el nivel no fucniona bien! No vuelve a pasar por Star ni por Awake!
 public class LevelManager : MonoBehaviour {
     public Text timeText;
     public Text scoreText;
     public Text lifeText;
     public Slider slider;
 
     public static LevelManager levelManager; //Singleton
 
     public int score = 0;
     private Animator animator;
     public float seconds = 0;
     float minutes = 0;
     int stars = 0;
 
     void Awake(){
         if (levelManager == null) { //Singleton
             levelManager = this;
         } else if (levelManager != this) {
             Destroy (gameObject);
         }
     }
 
     void Start(){
         Debug.Log ("Start");
         GameControl.Load (); 
         animator = GetComponent<Animator> ();
         lifeText.text = "x" + GameControl.lives;
     }
 }


To load my level I simply do:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class LoadSceneOnClick : MonoBehaviour {
 
     public void LoadByIndex(int sceneIndex)
     {
         SceneManager.LoadScene (sceneIndex);
     }
 }

 

Anyone know how to fix this? Is this maybe a bug? Thanks!

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

3 Replies

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

Answer by 5c4r3cr0w · Jan 11, 2017 at 01:04 PM

It's because you are creating a Singletone it will prevent Start() from getting called because GameObject calling Start() will get destroyed in Awake() and prevent duplication since gameobject form previous scene is not destroyed.

You should use OnLevelWasLoaded() instead of Start() as you need to call it when you load a new scene.

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 LK84 · Jan 11, 2017 at 01:27 PM 1
Share

OnLevelWasLoaded() has been deprecated. You shouldn't use it when writing new code.

avatar image Isaacg · Jan 14, 2017 at 12:18 AM 0
Share

I removed the Singleton pattern, made a few changes and now it seems to work. Thank you both!

avatar image
0

Answer by LK84 · Jan 11, 2017 at 01:25 PM

First of all Singletons (like Game Managers) should be applied to empty GameObjects. Plus, for a robust implementation of the singleton pattern in Unity have a look here: http://wiki.unity3d.com/index.php/Singleton All the other stuff not related to the Singletion (like the code in the Start() method) should be put in a seperated script on a seperated Game Object (which contains the relevant components) within the scene

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
0

Answer by AdmiralThrawn · Dec 20, 2017 at 08:24 PM

 void Start() { ... }

works perfectly fine for Singleton Monobehaviours.

For more professional infos continue to read here: singleton-monobehaviour-script

Cheers!

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

7 People are following this question.

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

Related Questions

Access singleton in OnEnable when it's initialized in Awake 3 Answers

Variables being initialized BEFORE Awake ()?? How is it possible? 1 Answer

Timer starts from wrong value on void Start or Awake? 1 Answer

crash bug 5.5 0 Answers

Access scripts on Awake or Start? 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