Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 iinot · Feb 24, 2016 at 01:31 AM · c#scene-loadingdatascore systemdonotdestroy

Passing Score from Game Scene to Game Over Scene usiing dontDestroyOnLoad C#

Hi guys please help, trying to save the score from game scene to gameover scene,

have this so far but doesn't work

This one below is from the game scene. I created an empty gameobject which obtains the score from the scoreboard which i didn't want to carry on to the next scene because it had to many other methods.

 public class CurrentScoreTracker : MonoBehaviour {

     public ScoreControl Score;
     public int score;
     // Use this for initialization
     void Awake () {
                Score.currentScore = score;
             DontDestroyOnLoad (transform.gameObject);

      }
 }

This one is the Scoreboard for the GameOver Scene

     public class GameOverScore : MonoBehaviour {

 public CurrentScoreTracker trackedScore;
 public int currentScore;
 public Text ScoreBoard;
 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     currentScore += trackedScore;
     ScoreBoard.text = "SCORE : "+currentScore.ToString ();
 }
  }


If this is not a good way to track score from one scene to another, please recommended me a better way.

Thanks in advance.

Comment
Add comment · Show 1
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 iinot · Feb 27, 2016 at 06:50 AM 0
Share

Any help would be appreciated.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali-hatem · Feb 27, 2016 at 12:06 PM

you have to carry the gameobject to the next scene if it have to many methods make another gameobject and attach a script with only dontdestroyonload method and score variable by the way you should add the gameobject to the next scene by drag and drop as a brefap .

  public class CurrentScoreTracker : MonoBehaviour {
     public static CurrentScoreTracker CST; //this line will give you access to this script without the ned of getcomponent. 
      public int score;
     
      void Awake () {
           if (CST== null) 
           {
            DontDestroyOnLoad(gameObject);
            CST= this;
           }
              else if (CST!= this)
              {
              Destroy(gameObject);
             }
       }
 

 //now increase the score from the GameOverScore script  :
  public class GameOverScore : MonoBehaviour {
  
  public int currentScore;
  public Text ScoreBoard;
  
  void Update () {
     currentScore +=1;
     CurrentScoreTracker.CST.score = currentScore;
      ScoreBoard.text = "SCORE : "+currentScore.ToString ();
  }
   }



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 iinot · Feb 28, 2016 at 07:24 AM 0
Share

That is what I did. I made another object obtaining the score from the script which has a lot of other methods. Right now I'm not sure on what to do because there is no object to drag into the inspector while in scene mode. The object only exist when it is in game view because of the dontdestroyonload method.

So the solution you are suggesting is to make a prefab and drag the prefab in the last scene and then drag it into the inspector?

So to confirm, which object am I making a prefab for. Is it the first game object from the game scene?

avatar image Ali-hatem iinot · Feb 28, 2016 at 09:18 AM 0
Share

yes the first game object from the game scene so make it a brefab and in any scene drag it back to the hierarchy but you should have another script in the game over scene to access the CurrentScorerTracker script to obtain the score value.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Add "1" to score text? Not "11111"? 1 Answer

Why does my script and collider2D automatically deactivate after reloading the scene? 0 Answers

Unity IAP Issues 0 Answers

how is it possible that my List has count = 3, but after im trying to compare onClick obhects name it says that my list is empty 0 Answers

Anyway To Set A Specific Part Of Code To A Scene? 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