• 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 Internetman · Mar 16, 2016 at 02:50 PM · c#timerscene-loadingloadlevelscenes

Save timer score between scenes? (C#)

Hello my fellow Unities, I have a timer in my game that's counting upwards, the timer is also used for score so 30 seconds = 30 points. Now I'm trying to figure out how to save the timer score, when switching scenes. Say that you survive for 40 seconds, now I want to save that float number and load it into the other scene? Note that I change scene when an enemy collides with the player.

I have my timer code here:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TimerScript : MonoBehaviour 
 {
     //Create timer
     public float timer = 0f;
 
     //Create displayed timer
     Text text;
     
     void Awake()
     {
         //Load Text class
         text = GetComponent<Text> ();
 
         //Reset the timer
         timer = 0f;
 
     }
 
     void Update()
     {
         //Start the timer and keep it going
         timer += Time.deltaTime;
 
         //Write out the timer on the screen with one decimal
         text.text = "" + timer.ToString("F1");
     }
 }

And here's the change scene script when an enemy "kills" the player:

 public void OnCollisionEnter2D(Collision2D col)
     {
         if(col.gameObject.tag == "Player")
         {
             Application.LoadLevel(3);
         }
     }
Comment
Add comment · Show 5
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 16, 2016 at 03:13 PM 0
Share

What should happen when you say "Load into another scene"? Is there a script attached to some object in that scene that uses the time / score you're sending?

If so, you could make some

 public static float currentScore;
 public static bool receivedScore;

values and assign them from your OncollisionEnter2D call above.

avatar image Internetman TreyH · Mar 16, 2016 at 03:30 PM 0
Share

It's supposed to be sending the score to the other scene, and by "score" i mean send the float number in the timer to the other scene. The timer is being shown visually on the screen with the "Text" variable and I want to save the data on how many seconds the player survived. Then stop the timer and send the float number from the timer to the other scene so it can be displayed.

avatar image TreyH Internetman · Mar 16, 2016 at 03:33 PM 0
Share

$$anonymous$$hm, but is there a script on some object in your other scene waiting to receive this value?

Show more comments
avatar image Verzoto · Mar 16, 2016 at 05:47 PM 0
Share

Hi!

You can save the score value using the PlayerPrefs. http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

4 Replies

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

Answer by Internetman · Mar 16, 2016 at 03:53 PM

Alright, so I kind of got it to work by using some good ol' IF statements, now the only problem is how do i stop the timer from counting when I'm in the next scene, and then reset it when going back again? (I have a reset button in the scene I'm loading in) If i write "Timer = 0" in the Awake fuction, it will be 0 in both scenes..

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TimerScript : MonoBehaviour 
 {
     //Create timer
     public static float timer = 0f;
 
     //Bool
     bool stopTimer = false;
 
     //Create displayed timer
     Text text;
     
     void Awake()
     {
         //Load Text class
         text = GetComponent<Text> ();
         DontDestroyOnLoad (this.gameObject);
 
         //Reset the timer
         //timer = 0f;
 
     }
 
     void Update()
     {
         //Start the timer and keep it going
         timer += Time.deltaTime;
 
         //Write out the timer on the screen
         text.text = "" + timer.ToString("F1");
 
         if(Application.loadedLevelName == "Scene 2 Game over")
         {
             text.text = timer.ToString("F1") + " Points";
         }
 
         if(Application.loadedLevelName == "Scene 1 Main Game")
         {
             
         }
     }
 
 }
Comment
Add comment · Show 3 · 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 Ali-hatem · Mar 16, 2016 at 04:07 PM 0
Share
      if(Application.loadedLevelName == "Scene 1 $$anonymous$$ain Game")
      {
          timer += Time.deltaTime;
      }
avatar image Internetman Ali-hatem · Mar 16, 2016 at 05:04 PM 1
Share

There we go, thank you it works at last! :D I wrote what you said and then created a Start function and wrote:

 void Start()
     {
         if(Application.loadedLevelName == "Scene 1 $$anonymous$$ain Game")
         {
             timer = 0.0f;
         }
     }
avatar image Ali-hatem Internetman · Mar 16, 2016 at 05:11 PM 1
Share

good job you are becoming more professional.

avatar image
2

Answer by Verzoto · Mar 16, 2016 at 05:47 PM

Hello, @Internetman!

You can save the score value using the PlayerPrefs

  PlayerPrefs.SetFloat("Player Score", 10.0F);

And you can get the score in the other scene using

  float score = 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
1

Answer by Mwarw · Mar 18, 2016 at 10:00 AM

If you save copy of timer as static variable, this should help

 static float timerCopy;
 function Start(){
 timer = timerCopy;
 }
 
 function Update(){
 timerCopy = timer
 }


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 Ali-hatem · Mar 16, 2016 at 03:17 PM

add this code to the TimerScript & make the object that this script attached to a brefab after that drag the object into the next scene.

 void Awake()
 {
 DontDestroyOnLoad(this.gameObject);
 }

 

Comment
Add comment · Show 1 · 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 Internetman · Mar 16, 2016 at 03:30 PM 0
Share

I'll try this! :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

How to fix Loading Screen? 0 Answers

Application.LoadLevel crashes unity 0 Answers

Can you help me to find a script for collision a sphere to an object and change the scenes in the same time ? 1 Answer

How to get scene name at certain buildIndex 5 Answers

First scene loads twice 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges