Search Unity

Restarting level after dying

Discussion in 'Scripting' started by iWoundPwn, Mar 23, 2013.

  1. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Hello I need help with a script, so basically you're an ice cube and you have to get to the finish line before you melt when you're done melting you kind of explode into water with this script
    Code (csharp):
    1. var restartLevel : String;
    2. var camera1 : Transform;
    3. var finished = false;
    4. var shrinkrate = 0.001;
    5.  
    6. function Start () {
    7.  
    8. }
    9.  
    10. function Update () {
    11.  
    12. if (transform.localScale.x > 0.5){
    13.  
    14. if (finished == false){
    15.  
    16. transform.localScale.x=transform.localScale.x-shrinkrate;
    17. transform.localScale.y=transform.localScale.y-shrinkrate;
    18. transform.localScale.z=transform.localScale.z-shrinkrate;
    19. camera1.localScale.x=camera1.localScale.x+shrinkrate;
    20. camera1.localScale.y=camera1.localScale.y+shrinkrate;
    21. camera1.localScale.z=camera1.localScale.z+shrinkrate;
    22.  
    23. }
    24.  
    25. }
    26. else
    27. {
    28.  
    29. transform.SendMessageUpwards ("GameOver");
    30.  
    31. }
    32.  
    33. }
    34.  
    35. function finish (){
    36.  
    37. finished=true;
    38.  
    39. }
    40.  
    41. function damage (damage:int){
    42.  
    43. transform.localScale.x=transform.localScale.x-damage;
    44. transform.localScale.y=transform.localScale.y-damage;
    45. transform.localScale.z=transform.localScale.z-damage;
    46. camera1.localScale.x=camera1.localScale.x+damage;
    47. camera1.localScale.y=camera1.localScale.y+damage;
    48. camera1.localScale.z=camera1.localScale.z+damage;
    49.  
    50.  
    51. }
    Is there any way so when the icecube is melted it restarts the scene you're playing in? thanks!
     
  2. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
  3. Negagames

    Negagames

    Joined:
    Dec 23, 2012
    Posts:
    17
    Well it seems that you already have a system for detecting if you have finished melting: if (transform.localScale.x > 0.5)
    All you need to do is add else{ Application.LoadLevel(1)} so something like this:

    if (transform.localScale.x > 0.5){
    if (finished == false){
    transform.localScale.x=transform.localScale.x-shrinkrate;
    transform.localScale.y=transform.localScale.y-shrinkrate;
    transform.localScale.z=transform.localScale.z-shrinkrate;
    camera1.localScale.x=camera1.localScale.x+shrinkrate;
    camera1.localScale.y=camera1.localScale.y+shrinkrate;
    camera1.localScale.z=camera1.localScale.z+shrinkrate;
    }
    }
    else{
    transform.SendMessageUpwards ("GameOver");
    yield WaitForSeconds (5); //So that the "Game over" SMU has time to work
    Application.LoadLevel(1); //Replace 1 with the level you want loaded when you loose, levels are arrainged in the build setup (Ctrl + Shift + B)
    }