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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Extra life at a certain score

Discussion in 'Scripting' started by SuperCrow2, Jul 2, 2018.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Idk what to do, do get it to work.

    I have two scripts

    Here's the score script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7.  
    8. public class Score : MonoBehaviour
    9. {
    10.  
    11.     //setting up the score to show in game
    12.  
    13.     public static int scorevalue = 0;
    14.     private static int scorevaule;
    15.     Text score;
    16.  
    17.     private int currentlivesvalue;
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         score = GetComponent<Text>();
    23.     }
    24.  
    25.  
    26.     void Update()
    27.     {
    28.         score.text = "Score " + scorevalue;
    29.  
    30.  
    31.  
    32.         if (Score.scorevaule == 100)
    33.         {
    34.  
    35.             currentlivesvalue += 1;
    36.         }
    37.  
    38.     }
    39. }
    the if statement didn't work I even tried to use greater than or equal to.



    Here's the current lives script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. //setting up current to show in-game
    7.  
    8. public class CurrentLives : MonoBehaviour
    9. {
    10.  
    11.     public static int currentlivesvalue = 3; //starts the game with 3 lives
    12.     Text currentlives;
    13.  
    14.    
    15.  
    16.  
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         currentlives = GetComponent<Text>();
    22.      
    23.     }
    24.  
    25.  
    26.     void Update()
    27.     {
    28.         currentlives.text = "Lives " + currentlivesvalue;
    29.  
    30.      
    31.        
    32.  
    33.     }
    34. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    The general pattern I use for this is as follows. You have the following variables:

    Code (csharp):
    1. int score;
    2. int lives;
    3. int bonus;
    4. const int bonusInterval = 10000;
    When you initialize your game, you do this:

    Code (csharp):
    1. void InitGame()
    2. {
    3.    score = 0;
    4.    lives = 3;
    5.    bonus = bonusInterval;
    6. }
    Then you have a function that adds points to the current score:

    Code (csharp):
    1. void AddPoints( int points)
    2. {
    3.    score += points;
    4.  
    5.    // and now we give lives for passing the bonus interval
    6.    if (score >= bonus)
    7.    {
    8.       lives += 1;
    9.       bonus += bonusInterval;
    10.  
    11.       // todo: maybe play a "Bonus Life!" sound here
    12.    }
    13. }
    You will have to adapt it to however you want to break your classes down, but the above will work every time and it is extremely simple.
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Thanks! should I make a brand new script for it? So I have one that shows and sets the score and current lives but then a third script for the extra lives?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    The computer scientists love to go nuts with a million different classes, but I prefer to keep it nice and simple and just put ALL the above code into whatever you want to call your "Game Manager" script.

    If you don't already have a Game Manager-ish kinda script, you might enjoy the benefit of having a single central class that controls the highest-level progress in the game: score, lives, bonuses, what level to play next, powerups, etc.

    All of those things are almost guaranteed to be VERY tightly-intertwined in any given game, so I like to keep them all close to each other in a single script.
     
  5. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Thanks for the advice! I am just starting off on game developing so its a relatively simple game. So like there's no power ups, no fancy animations, no fancy graphics, visual effects are very basic etc...
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Yeah, definitely don't let yourself get bogged down by too much concern for breaking things apart into small reusable parts (the entire point of classes). Just go with what works best for you because you will soon see what benefits might come from breaking certain things up. And when you see that, go back and break it up! That's why we call it 'software,' because it is soft and can be changed.
     
  7. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Right now I just made a new script for it because I still get confused when everything is part of the same script lol. I made the script but which object do I attach it too? my score or current lives object? or the object that you collect to get the extra life?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    What object is on doesn't matter. These scripts could be static classes if you want since they don't rely on Unity plumbing.

    If it is a separate script and not static, it now needs a reference to the script storing the score, and the script storing the lives.

    Also, not sure if an "extra lives" script should also have the .AddPoints() function in it, so now you can see precisely why multiple classes can paralyze you with indecision until you are comfortable with how to deal with this complexity.
     
  9. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Yeah my script name for score is "Score". I have to reference that correct?

    Then I called "Score" for the item collection script so it knows to increase your score after you collect it.
     
    Last edited: Jul 2, 2018
  10. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I can't get mine to work:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class ExtraLife : MonoBehaviour
    8. {
    9.  
    10.  
    11.     int scorevalue;
    12.     int currentlivesvalue;
    13.     int bonus;
    14.     const int bonusInterval = 200;
    15.  
    16.  
    17.     void InitGame()
    18.     {
    19.         scorevalue = 0;
    20.         currentlivesvalue = 3;
    21.         bonus=bonusInterval;
    22.     }
    23.  
    24.  
    25.  
    26.  
    27.  
    28.     void AddScore(int score)
    29.     {
    30.         scorevalue += score;
    31.        
    32.  
    33.      
    34.         if (scorevalue >= bonus)
    35.         {
    36.             currentlivesvalue += 1;
    37.             bonus += bonusInterval;
    38.  
    39.            
    40.         }
    41.     }
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you're calling that method from another script/class, make sure the 'AddPoints' method is public.
    If you need to 'get' the current score from elsewhere, you can make that public, as well, or use a property or a method to return the value of the variable.

    When you're adding to the score, you also have to make sure you have a reference to that ExtraLife script.

    One question, though: When you say it's not working, do you have any errors? Did you debug (debugger/Debug.Log) to see what part of the script was/wasn't working. Any other info to add? :)
     
  12. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    It was late at night so I rushed my post so I can get to bed then after I went to bed I remembered what I also wanted to say :D My score does show up and it does go up when its supposed to and the current lives shows up and losing a life works but getting the extra life doesn't work. I can try what you suggested here.
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe I can't see the whole picture but if the script you posted last is supposed to track this information, and display it, I do not see it.

    What you described just now sounds as though you have another script doing that (or more code in that one, that wasn't included).
     
  14. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Here's the two other scripts:

    This one you collect it and the score goes up

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class ItemCollected : MonoBehaviour
    8.  
    9. {
    10.  
    11.  
    12.     void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         if (other.name == "Player")
    15.         {
    16.             Destroy(gameObject);
    17.             Score.scorevalue += 100; //score increases by 100 after collecting it
    18.          
    19.  
    20.  
    21.         }
    22.     }
    23. }
    24.  

    And this one gets the score to show up in-game, I put in a text object from the UI:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. //setting up the score to show in game
    7.  
    8. public class Score : MonoBehaviour
    9. {
    10.  
    11.  
    12.  
    13.     public static int scorevalue = 0; //starts the score at 0
    14.     private static int scorevaule;
    15.     Text score;
    16.  
    17.  
    18. void Start()
    19.     {
    20.         score = GetComponent<Text>();
    21.     }
    22.  
    23.  
    24.     void Update()
    25.     {
    26.         score.text = "Score " + scorevalue;
    27.  
    28.  
    29.     }
    30. }
    31.  
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so with those scripts present, it looks as though the ExtraLife script has its own variables, and is not referring to the 'Score' script, the way it's written.
    If you gain your score when you collect items, you should call the method to AddScore.

    You can reconsider putting the code in the same script, or if not that, then make sure they have the references they need (and look up the right score to increase/show, etc).
    :)
     
  16. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Thanks for the reply! Which script should I add "AddScore" to? and will it go under void start or void update?
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I would imagine you'd want it when you enter the trigger? That looks like how you're adding to the score, but you are not calling the method that was proposed to you earlier for adding lives. You are using your original code, and the new script isn't being used.
     
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Again I recommend you move the lives, the score and the "add points" functionality all into the same script and actually get it working.

    THEN you can take a step back and break it into classes if it really still irritates you.

    Remember, as my Daddy always told me, "function first, object oriented silliness later."