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 /
This question was closed Mar 12, 2017 at 06:19 AM by Ben_Huang4163 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Ben_Huang4163 · Mar 07, 2017 at 03:55 AM · score systemscene loadlevelload

how to use SceneManager.sceneLoaded replacing OnLevelWasLoaded

Hello: I found that the score points in my project is kept even if i restart the game or reload the current scene. the score points is kept in a static variable. here is the script:

 public class ScoreManager : MonoBehaviour {
 
     public static int score;
     Text text;
 
     void Awake()
     {
         text = GetComponent<Text>();
         score = 0;
     }    
     
     void Update () {
         text.text = "Score: " + score;
     }

now I want to clear that score points every time i restart my game. I tried to search a way to solve this.

at first, i found this method:

 void OnLevelWasLoaded(int level)
     {
         
         if (level==0)
         {
               ScoreManager.score = 0;
         }
       
     }
 

but it looks like the unity have abandoned this function after version 5.3 (i'm using unity 5.5). Then i found the SceneManager.sceneLoaded method, i added those lines to my script:

  void OnEnable()
     {
         SceneManager.sceneLoaded += Loadedscene;
     }
 
     void OnDisable()
     {
         SceneManager.sceneLoaded -= Loadedscene;
     }
 
     void Loadedscene(Scene scene,LoadSceneMode mode)
     {
        
         if (scene.name=="Start")
         {
             ScoreManager.score = 0;
         }
     }

but it didn't work as i expected.

can anyone help me to solve this, please? thank you.

Comment
Add comment · Show 4
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 TreyH · Mar 07, 2017 at 04:18 AM 0
Share

what happens when you print the name of the scene passed by that event?

avatar image Ben_Huang4163 TreyH · Mar 07, 2017 at 09:08 AM 0
Share

nothing happens, nothing changes.

avatar image TreyH Ben_Huang4163 · Mar 07, 2017 at 11:55 AM 0
Share

So,

 void Loadedscene(Scene scene,LoadScene$$anonymous$$ode mode)
      {
         Debug.Log (scene.name, scene.buildIndex);
      }

doesn't print or do anything? Are you sure it's being subscribed? Does the below print anything if you put that before your subscription?

 void OnEnable ()
     {
         Debug.Log ("I am trying to subscribe!");
     }

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by herDev · Mar 07, 2017 at 08:16 AM

Here you go:

         Scene scene = SceneManager.GetActiveScene();

         if (scene.buildIndex == insertSceneBuildIndexHere) {
         //Do something
         }
 or
         if (scene.name == insertSceneNameHere) {
         //Do something
         }
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 Ben_Huang4163 · Mar 07, 2017 at 09:28 AM 0
Share

thanks. i followed your advice, the script:

  void OnEnable()
     {
         Scene$$anonymous$$anager.sceneLoaded += Loadedscene;
     }
 
     void OnDisable()
     {
         Scene$$anonymous$$anager.sceneLoaded -= Loadedscene;
     }
 
     void Loadedscene(Scene scene,LoadScene$$anonymous$$ode mode)
     {
         scene = Scene$$anonymous$$anager.GetActiveScene();
       
         if (scene.buildIndex==0)
         {
             Score$$anonymous$$anager.score = 0;
         }
     }

but it didn't work. the score is still store into the static variable after my character died in game. maybe i didn't do it right.

avatar image tacman1123 Ben_Huang4163 · Oct 09, 2020 at 01:06 PM 0
Share

I don't think you need

 scene = Scene$$anonymous$$anager.GetActiveScene();

because you're passing the scene in already.

Follow this Question

Answers Answers and Comments

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

my counter wont change from 0 help 1 Answer

How to Switch GameObjects (Weapons) by reaching a specific amount of score? 0 Answers

multiple score counts UI 0 Answers

Facebook score API not saving score for public user 1 Answer

How to add a tally system to a Tic Tac Toe game 0 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