Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Health player help

Discussion in 'Getting Started' started by heenriquue, Feb 12, 2015.

  1. heenriquue

    heenriquue

    Joined:
    Feb 12, 2015
    Posts:
    8
    have a system of three lives in my game however I want to add the proviso that when you lose a life , reset the level , but with a life less , but when you restart the level life also restarts?
     
  2. AlexConnolly

    AlexConnolly

    Joined:
    Dec 23, 2012
    Posts:
    32
    Defero likes this.
  3. heenriquue

    heenriquue

    Joined:
    Feb 12, 2015
    Posts:
    8
    sorry friend, i have system of life with three texture my code life is:
    Code (JavaScript):
    1. #pragma strict
    2. public var vidaAtual : Texture2D[];
    3. static var vidas : int;
    4. private var contador : int;
    5. static var lose_count : int;
    6.  
    7. function Start () {
    8.     guiTexture.texture = vidaAtual[0];
    9.     vidas = vidaAtual.Length;
    10.    
    11. }
    12. function Update () {
    13. }
    14.  
    15. function ExcluirVida(): boolean {
    16. if (vidas < 0){
    17.  
    18. return false;
    19. }
    20.     if (contador < (vidas - 1)){
    21.         contador += 1;
    22.         guiTexture.texture= vidaAtual[contador];
    23.         return true;
    24.         }else{
    25.             return false;
    26.         }
    27. }
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Even though vidas is static, you're still setting its value based on vidaAtual's length at the start of the scene. So essentially, you're overwriting however many lives you should have with the number of images in that array no matter what, right?

    Why not just set the number of lives in the inspector and subtract from that static value on loss of life?
     
  5. heenriquue

    heenriquue

    Joined:
    Feb 12, 2015
    Posts:
    8
    Friend excuse , would be a little more specific, my English sucks , i am brasilian
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Bah. I was wrong. I forgot that static variables aren't inspectable. Plus, static variables are, in general, something to avoid.

    Among all the other nuggets of golden information Unity provides, there is a lesson on persistent data that I will be watching as soon as the boss turns his back. I've got a test project set up that I'll share the code with you once I have it working in a simple way.
     
  7. heenriquue

    heenriquue

    Joined:
    Feb 12, 2015
    Posts:
    8
    I will be grateful
     
  8. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Really? I use them sometimes. I am curious, what is wrong with them? Do they just cause confusion or something?
     
  9. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51
    Store your life variable on a game controller, and set it to not destroy. This way the controller persists scene loads.

    Code (csharp):
    1. DontDestroyOnLoad (gameObject);
     
  10. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It's something of a good-practice kind of thing. I'm thinking it's along the same lines as to why you're not supposed to declare global level variables in Javascript. To be honest, for small tasks I don't see them being a problem at all, but others may know more than me on that subject.

    That's what I was going to recommend (and it WILL be the right solution), but by itself, if not structured properly, will cause duplicate objects to be created and lost references to variables, or in this case, the assigned textures and such. Unfortunately I had social plans I didn't know about tonight (ah, marriage), so I didn't get around to figuring this out. I'll take a look at it tomorrow at some point.
     
    calmcarrots likes this.
  11. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    Ah thanks! It's great to learn this kind of stuff
     
  12. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51
    What I normally do is to create my gameController as a singleton, that way only one instance of it is ever created.

    Code (csharp):
    1.  
    2. public class GameController : MonoBehaviour
    3. {
    4. public static GameController instance;
    5.  
    6. void Awake ()
    7. {
    8. if (instance == null) {
    9. instance = this;
    10. DontDestroyOnLoad (gameObject);
    11. } else {
    12. Destroy (this); // or gameObject
    13. }
    14. }
    15.  
    Then, any time you would reference a local variable in the gamecontroller, you would by
    Code (csharp):
    1. instance.Myvariable;
    or in any other script, at anytime by using

    Code (csharp):
    1. GameController.instance.Myvariable;
    Hopefully this helps you out. Happy Coding.
     
    Schneider21 likes this.
  13. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @dvirus1023's answer is exactly what I would have posted if I hadn't gotten busy today. This is what I did in my unfinished Hnefatafl game to manage the game state, and it worked nicely.
     
  14. heenriquue

    heenriquue

    Joined:
    Feb 12, 2015
    Posts:
    8
    very thanks i right
     
  15. heenriquue

    heenriquue

    Joined:
    Feb 12, 2015
    Posts:
    8
    all right
    I'm with the points system and wanted to save a score for that stage when to recomme - points continue where you left off ,. and reset only when it is to the game screen over.
     
  16. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @heenriquue Same concept as tracking lives. Just throw 'score' in as a variable in the GameController class, and add to it as appropriate. It won't reset until you tell it to (when player's lives <= 0).