Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ending game based on score

Discussion in 'Scripting' started by Velketor, Oct 26, 2007.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Right now I'm trying to end the game based on your score, which is changed by a few other scripts I have, and they're working fine. The problem I have is that when I try to add to the score script, even though the variables change, nothing else seems to happen.
    Here's the current code:
    Code (csharp):
    1.  
    2. var cam : GameObject;
    3. var Win : GameObject;
    4. var Lose : GameObject;
    5. var score = 0;
    6. var winscore : int;
    7. var losescore : int;
    8.  
    9. function update (){
    10. if (score < winscore){
    11.     Win.active=false;
    12.     }
    13. else if(score >= winscore){
    14.     Destroy(cam);
    15.     Win.active=true;
    16.     }
    17.  
    18. if (score <= losescore){
    19.     Destroy(cam);
    20.     Lose.active=true;
    21.     }
    22. else if(score > losescore){
    23.     Lose.active=false;
    24. }
    25. }
    26.  
    27. function AddToScore () {
    28.    
    29.  
    30.  ++score;
    31.    
    32.  
    33.  guiText.text = "Score:   " + score.ToString ();   
    34.  
    35.  
    36. }
    37.  
    38. function SubtractFromScore(){
    39.  
    40.  
    41.  
    42. --score;
    43.  
    44. guiText.text = "Score:    " + score.ToString ();
    45.  
    46. }
    47.  
    There are no errors, but nothing happens when the score variable exceeds the winscore, or falls below the losescore. Also, the gui texts attached to Win and Lose are always active, any ideas to correct the problem?
    Any advice is appreciated.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Presumably you mean for your "update" function to be called "Update". Otherwise it's just a regular function and isn't going to be called every frame like Update is.

    --Eric