Search Unity

Replay...?

Discussion in 'Scripting' started by cdesmond, Aug 21, 2005.

  1. cdesmond

    cdesmond

    Joined:
    Jul 26, 2005
    Posts:
    18
    How would I go about having a game restart after a certain amount of time or score? I have for instance tried:
    Code (csharp):
    1.  
    2. if (timeLeft == 0){
    3.     Application.LoadLevel (0);
    4. }
    But it seems to just hang...?

    Thanks in advance...
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Have you tried this in 1.1rc1?
    There were a few bugs in 1.0.4 releated to level loading.
     
  3. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Application.LoadLevel() seems to work pretty good for me in RC1...
     
  4. cdesmond

    cdesmond

    Joined:
    Jul 26, 2005
    Posts:
    18
    I tried this on the "Simple Unity Tutorial" using 1.1rc1 and it reloads the level but everything hangs as though it's waiting to be initiated or as though the scripts are missing... could it be the way I am calling it? Here is the enitre code below:

    Code (csharp):
    1.  
    2. var startTime = 30.0;
    3. function Update () {
    4. // The time left for player to complete level!
    5. timeLeft = startTime - Time.time;
    6. // Don't let the time left go below zero.
    7. timeLeft = Mathf.Max (0, timeLeft);
    8. // Format the time nicely
    9. guiText.text = FormatTime (timeLeft);
    10. if (timeLeft == 0){
    11.     Application.LoadLevel (0);
    12.     }
    13. }
    14. // Format time like this
    15. // 12[minutes]:34[seconds].5[fraction]
    16. function FormatTime (time)
    17. {
    18. var intTime : int = time;
    19. var minutes : int = intTime / 60;
    20. var seconds : int = intTime % 60;
    21. var fraction : int = time * 10;
    22. fraction = fraction % 10;
    23. // Build string with format
    24. // 12[minutes]:34[seconds].5[fraction]
    25. timeText = minutes.ToString () + ":";
    26. timeText = timeText + seconds.ToString ();
    27. timeText += "." + fraction.ToString ();
    28. return timeText;
    29. }
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Maybe it is trying to reload the level every frame?
    Add a Debug.Log ("Loading level"); just before the Load level call.
     
  6. cdesmond

    cdesmond

    Joined:
    Jul 26, 2005
    Posts:
    18
    I added the Debug.log... and it looks like game does not reset the time. I tried to reset the variable of StartTime to 30.0, but it does not seem to work... any help here would be appreciated as I am an absolute beginnner just looking to get some good basics out of the way.

    Thanks for all the help.
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Time.time is the time since startup of the game.
    Try implementing the Awake function and store when the level was loaded in a member variable of the script.
    Code (csharp):
    1.  
    2. var startupTime = 0.0;
    3. function Awake ()
    4. {
    5.    startupTime = Time.time;
    6. }
    7.  
    When you do the countdown timer, you have to subtract startupTime from Time.time
     
  8. cdesmond

    cdesmond

    Joined:
    Jul 26, 2005
    Posts:
    18
    Thanks Joachim!
    I played a little with this this morning and decided I should go to work and not be late (as unity has a tendency to make me late each morning lately ;)), so I of course couldn't get it to work... I think I understand the Awake () function and what needs to happen. Thanks for the reply. BTW, I tried the "game" on the PC and in some cases the playback is okay and others it has some serious slow down. I am not really a PC guy, but it may have been the Graphics Cards for the slower machines.

    I have included a screenshot of the "simple unity tutorial" with this post if you are interested in seeing it. I am sure I will have more questions as I get the bigger pictures... thanks again for the help.
     

    Attached Files:

  9. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    This doesn't seem to reset time for me, Joe, after I reload the level. If I start with 60 seconds, finish the level in 30, wait 15 seconds to reload the game, then reload it the timer in game shows only 15 seconds remaining.

    Wouldn't it just be easier to create a variable called secondsLeft = 60 and in one of the function Updates remove 1 from it's value using each second?
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Yes that would indeed be easier.
    Like so.

    Code (csharp):
    1.  
    2. var secondsLeft = 60.0;
    3. function Update ()
    4.   secondsLeft -= Time.deltaTime;
    5.   if (secondsLeft < 0)
    6.    Application.LoadLevel ("Level");
    7. }
    8.  
     
  11. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Sorry to correct you, but I believe that should be: var secondsLeft = 60.0;

    That way it infers as a float and not an int. I know you were typing in a hurry between programming. :)

    -Jon
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Thanks, Jon.
    (Corrected the script in the post above)
     
  13. junyx

    junyx

    Joined:
    Aug 26, 2005
    Posts:
    12
    This thing is driving me crazy. First of all I preface that I'm using the 1.1 stable version and everything works fine in the editor, the problem occurs only in stand alone (both app and widget).
    I have a main menu with a script that uses LoadLevel() to load the first level. The game over script brings back to the main menu and that's fine. But when the menu script loads the first level again the level is without any script :?
    Could someone provide a possible word of explanation for that?
    I would post some code, if only I could know which part I should post :p

    Thank you very much
     
  14. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We consider any discrepancy of editor vs player a bug in unity.
    We want people to rely on that it will just work in the player, no questions asked.

    Please report a bug on this and attach your project, i will take a look at it immediately. (Use Report Bug.app)
     
  15. junyx

    junyx

    Joined:
    Aug 26, 2005
    Posts:
    12
    Thank you, I read your post only now. I'll send a bug report shortly.
    OTEE guys definitely rock my world :D