Euras

PlayerLives

Feb 1st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. public class PlayerLives : MonoBehaviour {
  8.  
  9.     public Image heartPrefab;
  10.     Image newHeart;
  11.     public Transform canvas;
  12.     public List<Image> imgs = new List<Image>();
  13.     RectTransform rT;
  14.     public static bool loseLife = false;
  15.  
  16.     void Start ()
  17.     {
  18.         DrawHearts();
  19.     }
  20.  
  21.     void Update()
  22.     {
  23.         if(loseLife)
  24.         {
  25.             if(imgs.Count > 0)
  26.             {
  27.                 imgs.RemoveAt(imgs.Count - 1);
  28.                 if (canvas.childCount != 0)
  29.                 {
  30.                     if (canvas.GetChild(3).GetComponent<Image>())
  31.                     {
  32.                         Destroy(canvas.GetChild(canvas.childCount - 1).gameObject);
  33.                     }
  34.                 }
  35.                 loseLife = false;
  36.             }
  37.         }
  38.     }
  39.  
  40.     public void DrawHearts()
  41.     {
  42.         for (int i = 0; i < MainGlobals.lives; i++)
  43.         {
  44.             newHeart = Instantiate(heartPrefab) as Image;
  45.             newHeart.transform.SetParent(canvas, false);
  46.             newHeart.rectTransform.anchoredPosition = new Vector2(-20 - (i * 25), -20);
  47.             imgs.Add(newHeart);
  48.         }
  49.     }
  50. }
Add Comment
Please, Sign In to add comment