Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Killbert · Apr 04, 2014 at 12:31 PM · scoredontdestroyonload

Keeping score problems dontdestroyonload

Ok I have been reading all articles here regarding keeping scores and still I can't get it to work properly. I just do not understand the right syntax on how to implement things. I am sure I am nearly there, but I get lost when trying to implement things.

I have a C# script that handles my game logic for a level:

First, I set my level variables:

 public class GameLogic : MonoBehaviour {
 
     public GUIText countText;
     public GUIText targetCity;
     public GUIText gameOverText;
     public GUIText endScoreText;
     public GUIText timerText;
     public Texture2D bgImage;
     private int count;
     public GameObject cityPrefab;
     List<MyCity> mycities;
     public float finalScore; // I WANT THIS VALUE TO BE AVAILABLE IN MY NEXT LEVEL.
     private float startTime;
 

//Rest of script executes fine; //Then, my level runs until the conditions for ending the game have been met (in this case = Time's up):

 public void GameOver ()
     {
         gameOverText.text = "Time is Up!";
         endScoreText.text = "You have found " + count.ToString() + " cities, great work!";
         Destroy (GameObject.FindWithTag("EmptyCity"));
         Destroy (GameObject.FindWithTag("City"));
         Destroy (GameObject.FindWithTag("TargetCity"));
         finalScore = count; //SO I KNOW HAVE A VALUE FOR THE FINAL SCORE OF THE LEVEL. 
 
 
 // THE GAME PAUSES FOR 7 SECONDS, THEN LOADS THE NEXT SCENE. HOW DO I GET THE VALUE OF finalScore in my next scene?
 
         StartCoroutine(Wait());
 
     }
 
 
 
     IEnumerator Wait() {
 
         yield return new WaitForSeconds(7);
         Application.LoadLevel (3);
     }
 

Level3, is just a simple scene where I congratulate the player with his performance. This is where I fail to get what I need to do to have the public float 'finalScore' from this level and load it into a GUIText object and display it on screen in my new scene.

I know I need to use Dontdestroy onload, but I fail to understand how that works when dealing with my public float finalScore.

Where I go wrong is linking the public float to a gameobject and making that game object available via dontdestroyonload to my next scene.

As I have tried so many things already, I would greatly appreciate if you could help me with the exact code lines I need to to this. Otherwise I am afraid that I make a mistake in the syntax.

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 Killbert · Apr 04, 2014 at 03:58 PM 0
Share

Thanks Simon. I went that route and it works like a charm now. Still, I do want to learn about the Gameobject / dotndestroyonload approach at some point, but for now, this will do perfectly fine!

2 Replies

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

Answer by Simon-Larsen · Apr 04, 2014 at 12:43 PM

Okay, so as I can understand you want you save a highscore so you can access it even after you switch scenes. Now, this is of course possible using the DontDestroyOnLoad method, but instead I recommend you use PlayerPrefs. Think of PlayerPrefs as a way to save values outside of your scenes. For your highscore you could set its value in your first scene like so:

 void SetScore() {
     PlayerPrefs.SetFloat("Player Score", 10.0F);
 }

You can then acces it in whichever scenes you want to access it from by using the GetFloat method, like so:

 void GetScore() {
     print(PlayerPrefs.GetFloat("Player Score"));
 }
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 ABdoudj · Sep 05, 2018 at 09:12 AM

Thanks, i had the same problem with the normal score, when loading the same scene for a next round, i was struggling to make the Dontdestroyonload ,, but it didnt work , so i think i am gonna go with the playerprefs method with both normal score as well as the highscore But with different Keys,, and when the player score is below zero i will restart the scene BUt this time delete the stored key for the normal score ( in my case points) , that will be in a seperate function ! ;) Have fun Guys . . .

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

22 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

Related Questions

DontDestroyOnLoad reset at certain point 2 Answers

DontDestroyOnLoad reset the gameobject 1 Answer

Don't understand how to use DontDestroyOnLoad. 2 Answers

score broken, score loads on new scene but then errors 0 Answers

GetComponent doesn't work 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