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 Zypher · Jun 10, 2014 at 04:11 AM · timerscoregameover

Time Score?

Okay, this is the only thing that is hindering the completion of my game. I have a timer script:

 #pragma strict
 private var startTime : float;
 var textTime : String;
 //First define two variables. One private and one public variable. Set the first variable to be a float. 
 //Use for textTime a string. 
 function Start() {
 startTime = Time.time;
 }
 function OnGUI () {
 var guiTime = Time.time - startTime; 
 //The gui-Time is the difference between the actual time and the start time.
 var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
 var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
 var fraction : int = (guiTime * 100) % 100;
  
  textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
 //text.Time is the time that will be displayed.
  GetComponent(GUIText).text = textTime; 
 }
  
 var bestScore = 0;
 var playerTime = 0;
 //When the race is completed;
 bestScore = playerTime;
 //Reset playerTime to zero
 //Display the best score(Score to beat)
 //If the next drivers playerTime is higher than the previous bestScore
 if(playerTime > bestScore){
   //Display the new best score.
  //Reset playerTime to zero
 }

The player's job in my game is to tap each of the spawning stars to destroy them, and if they let one pass a certain point they lose the game. The Timer is there to basically see how long each player can last.

I've tried, but I cannot seem to save the timer score and display on the GameOver Scene. How do I go about this? Could you please explain in a step by step way.

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

1 Reply

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

Answer by IvovdMarel · Jun 10, 2014 at 05:49 AM

When you change scenes, all old objects of your scene will be destroyed. In order to keep your objects or scripts, you will have to use a DontDestroyOnLoad - method in your class.

http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

The data that you store in that class, can be accessed from another class. The easiest way to do this, is by creating a public static variable.

  • Create PersistentGameData.js

  • Add the script to an empty gameobject

  • Add DontDestroyOnLoad in the Awake method

  • Create a public static var gameScore.

  • Set it by using PersistentGameData.gameScore = bestScore

  • You can now access this variable anywhere, anytime, even after the scene changed.

Good luck!

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

Timer Score display on Game Over Scene? 1 Answer

Loading a score on a scene 2 Answers

Score and Level Loading 1 Answer

Get score in areas 0 Answers

Why can't I call function from another script? 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