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 Ishkur · Jul 21, 2012 at 02:07 AM · timesceneslevelsapplication.loadlevelpersistence

Keeping track of time after calling Application.LoadLevel

Hello, I'd like some advice and a general direction for how to implement a certain game mechanic. I'm not sure if my current methods are viable or not.

I have a scene with a GameObject myGameObject which uses DateTimes and TimeSpan for a timer to execute a method once the TimeSpan reaches zero.

I would like the player to be able to move to a different scene where myGameObject isn't present on the screen, however still have the timer count down and the method executed as time passes.

Using Application.LoadLevel to move the player to a new level/scene destroys all current GameObjects and my countdown method is interrupted.

DontDestroyOnLoad() doesn't seem helpful as the new scene should not have myGameObject present.

Will I have to redesign my game to not require LoadLevel to load new levels, and instead create my own level loading script that operates in a single scene?

Is there a way to continuously keep track of time and execute the method even after Application.LoadLevel has been called?

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
0

Answer by mindlube · Jul 21, 2012 at 03:32 AM

DontDestroyOnLoad : "Makes the object target not be destroyed automatically when loading a new scene. " That's what you want right? If that doesn't work for you, or it's too complicated. (it does add complexity to your scenes) Then sounds like a good use case for PlayerPrefs! Just drop this component into some gameobject in every scene you want to try the time in. Hope this helps

 using UnityEngine;
 using System;
 
 public class TimeTracker : MonoBehaviour 
 {
  public DateTime LastTime { get; set; }
  
  public static string PrefsKey = "last time";
  
  public void Awake()
  {
  if(PlayerPrefs.HasKey(PrefsKey))
  {
  var ticks = long.Parse( PlayerPrefs.GetString( PrefsKey ) ); 
  LastTime = new DateTime( ticks );
  } 
  else
  {
  LastTime = DateTime.Now;
  }
  }
  
  public void OnDestroy()
  {
  PlayerPrefs.SetString( PrefsKey, LastTime.Ticks.ToString() );
  PlayerPrefs.Save();
  }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

time between scenes (loading time) 0 Answers

I need help for delete old highscore 0 Answers

Quit application before relaunching to specific scene 1 Answer

Problems editing a level from script 1 Answer

Loading A Level After 40 Seconds 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