Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hi I have a question

Discussion in 'Scripting' started by branek42, Feb 5, 2015.

  1. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    Code (JavaScript):
    1.  var key : GameObject;
    2. var date : System.DateTime;
    3. //sets something allowing you to populate it with the date and time
    4. var text : String;
    5. var text2 : String;
    6. var hodiny : GUIText;
    7. function Update()
    8. {
    9.      //tells the var to look for the precise moment you are at
    10.      var date = System.DateTime.Now;
    11.      //converts the above var to a readable string
    12.      text = date.ToString("HH:mm:ss");
    13.      //prints it to inspector
    14.      print (text);
    15.      hodiny.text =      text ;
    16.    if (text == text2){
    17.       key.SetActive(true);
    18.      
    19.       }}
     
    Last edited: Feb 5, 2015
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I don't understand the question.
     
    Whalepork likes this.
  3. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    i need create time to went although is game turn off

    to went game time on background
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I remember you!
    Sorry, branek42, but as with last time, nobody can understand what you are saying. I don't know what your native language is, but you might try searching for unity forums in whatever langauge that is.

    Anyway, if you're asking how to run the above code when the game is turned off, you can't do that.
    IF you're asking how to load the main level menu when the game is started, it's as simple as putting the MainMenu scene as the first scene in the game.
     
  5. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    in code was bug ...I want to went time during turn off game to went game time on background
     
  6. Jon_Dev

    Jon_Dev

    Joined:
    Jul 22, 2012
    Posts:
    4
    I think he is asking for the time difference from when the game was last played to the time of a new instance of it being played
     
  7. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Looking at the script and then looking at the posts makes me think this is a copy/paste deal and someone doesn't know what they're doing.

    http://unity3d.com/learn
     
    BenZed likes this.
  8. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    the point is that my flower grew at a particular time

    but when the time(text2) so it skips setactive not working
     
  9. sean4mvo

    sean4mvo

    Joined:
    Jan 5, 2015
    Posts:
    15
    You might want to take frames and save it to memory so it can revert to it and you can access it that way, I don't have the knowledge for that but keep looking..HTH
     
  10. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    hi why I added at a specified time more day as one ?


    Code (JavaScript):
    1.  
    2. var key : GameObject;
    3. var date : System.DateTime;
    4. //sets something allowing you to populate it with the date and time
    5. var text : String;
    6. var text2 : String;
    7. var hodiny : GUIText;
    8. var days : int = 0;
    9.  
    10.  
    11.    
    12.  
    13. function Update()
    14. {
    15.    
    16.     days = PlayerPrefs.GetInt("Days");
    17.    
    18.     //tells the var to look for the precise moment you are at
    19.      var date = System.DateTime.Now;
    20.      //converts the above var to a readable string
    21.      text = date.ToString("HH:mm:ss");
    22.      //prints it to inspector
    23.      print (text);
    24.      hodiny.text =      text ;
    25.      
    26.      
    27.      if (text== "03:45:40"){
    28.       days =days + 1;
    29.       PlayerPrefs.SetInt("Days" ,days);
    30.    
    31.       key.SetActive(true);
    32.      
    33.      
    34.       }}
    35.  

    I think this ( day =day + 1; )
     
    Last edited: Feb 8, 2015
  11. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    You need to add some extra text to explain what you're actually doing!
     
  12. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    Code (JavaScript):
    1. var plant : GameObject;
    2. var date : System.DateTime;
    3. //sets something allowing you to populate it with the date and time
    4. var text : String;
    5. var clock : GUIText;
    6. var days : int = 0;
    7.  
    8. function Update()
    9. {
    10.  
    11. if (days == 1) {
    12.    plant.SetActive(true);        
    13. }
    14.    
    15. days = PlayerPrefs.GetInt("Days");
    16.  
    17.     //tells the var to look for the precise moment you are at
    18.        var date = System.DateTime.Now;
    19.     //converts the above var to a readable string
    20.        text = date.ToString("HH:mm:ss");
    21.     //prints it to inspector
    22.        print (text);
    23.        clock.text =      text ;
    24.    
    25.    
    26. if (text== "24:00:00"){
    27.       days = days + 1;
    28.       PlayerPrefs.SetInt("Days" ,days);
    29. }}
    30.  


    I need plus 1 day at a specified time ..... days = days + 1; but I added 63 days I think it's one hundredths
     
  13. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76
    Please help me .....
     
  14. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    107
    Code (CSharp):
    1. if (text== "24:00:00"){
    2.       days = days + 1;
    3.       PlayerPrefs.SetInt("Days" ,days);
    4. }
    If your game is not ON at 12:00, it will never calculate "day+1". And if it is ON, time will be equal to "24:00:00" for entire second and during this second your date will be doing +1 over and over again so it will be like day 100 very fast.
    This is how I would do it ( I don't know Java, so watch for mistakes):

    Code (JavaScript):
    1. void Start(){
    2. var LastPlayedDay = PlayerPrefs.GetInt("SavedDay");
    3. var days = PlayerPrefs.GetInt("Days");
    4. var date = System.DateTime.Now;
    5. if (date.Day!=LastPlayedDay)
    6. days++;
    7. }
    And when you are turning the game off save the day you played:

    Code (JavaScript):
    1. void MySaveFunction(){
    2. var date = System.DateTime.Now;
    3. PlayerPrefs.SetInt("SavedDay", date.Day);
    4. PlayerPrefs.SetInt("Days", days);
    5. }
    This will only work if you start your game every day. And before this can work you need to set "SavedDay" to day you started playing so a small modification:

    Code (JavaScript):
    1. void Start(){
    2. var LastPlayedDay = PlayerPrefs.GetInt("SavedDay");
    3. var days = PlayerPrefs.GetInt("Days");
    4. var date = System.DateTime.Now;
    5.  
    6. var NotFirstTime = PlayerPrefs.GetInt("NotFirstGame");
    7. if (NotFirstTime==0){
    8. LastPlayedDay=date.Day;
    9. PlayerPrefs.SetInt("SavedDay", date.Day);
    10. PlayerPrefs.SetInt("NotFirstGame", 1);
    11. }
    12. if (date.Day!=LastPlayedDay)
    13. days++;
    14. }
    Again, I am not sure if this will work, haven't tested, and if your game is off for few days you will need a function to calculate how much time passed. But I hope this will help you for a start. And if you want date to change while game is on move some code that checks for day change to Update function.
     
    Last edited: Feb 12, 2015
  15. branek42

    branek42

    Joined:
    Oct 10, 2013
    Posts:
    76