Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Is there a better way to set up a stock health system for my 8-player local/couch co-op game?

Discussion in 'Multiplayer' started by HeartOfGold, Mar 5, 2023.

  1. HeartOfGold

    HeartOfGold

    Joined:
    Dec 21, 2017
    Posts:
    1
    Hi all,

    New dev here working on a small mini-game game. Not the most experienced with programming and Unity.

    I'm trying to implement a stock health system similar to Super Smash Bros. I've set up a canvas with eight player health displays that are disabled then activated based on the number of players playing. The behaviour works right now with eight unique displays and 5 hardcoded lives. But ideally I'd just have one player health display prefab and any number of lives set by the options, but I'm not sure how to set that up. Any obvious ways that I could improve this code?

    Thank you.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthManager : MonoBehaviour
    6. {
    7.     public int playerStartLife = 5;
    8.     public int[] playerLife = new int[8];
    9.  
    10.     public GameObject gameOver;
    11.  
    12.     public GameObject[] playerHealthDisplay;
    13.     public GameObject[] player1Hearts;
    14.     public GameObject[] player2Hearts;
    15.     public GameObject[] player3Hearts;
    16.     public GameObject[] player4Hearts;
    17.     public GameObject[] player5Hearts;
    18.     public GameObject[] player6Hearts;
    19.     public GameObject[] player7Hearts;
    20.     public GameObject[] player8Hearts;
    21.  
    22.     private void OnEnable()
    23.     {
    24.         SpawnManager.OnPlayerStart += SetPlayerHealthDisplay;
    25.         PlayerController.OnPlayerDeath += HurtPlayer;
    26.     }
    27.  
    28.     private void OnDisable()
    29.     {
    30.         SpawnManager.OnPlayerStart -= SetPlayerHealthDisplay;
    31.         PlayerController.OnPlayerDeath -= HurtPlayer;
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         if(GameObject.FindGameObjectsWithTag("Player").Length <= 1)
    38.         {
    39.             gameOver.SetActive(true);
    40.         }
    41.     }
    42.  
    43.     public void SetPlayerHealthDisplay(int playerCount)
    44.     {
    45.         for (int i = 0; i < playerCount; i++)
    46.         {
    47.             playerLife[i] = playerStartLife;
    48.             playerHealthDisplay[i].SetActive(true);
    49.         }
    50.     }
    51.     public void HurtPlayer(int playerIndex)
    52.     {
    53.         playerLife[playerIndex] -= 1;
    54.  
    55.         switch (playerIndex)
    56.         {
    57.             case 0:
    58.                 for (int i = 0; i < player1Hearts.Length; i++)
    59.                 {
    60.                     if (playerLife[playerIndex] > i)
    61.                     {
    62.                         player1Hearts[i].SetActive(true);
    63.                     }
    64.                     else
    65.                     {
    66.                         player1Hearts[i].SetActive(false);
    67.                     }
    68.                 }
    69.                 break;
    70.             case 1:
    71.                 for (int i = 0; i < player2Hearts.Length; i++)
    72.                 {
    73.                     if (playerLife[playerIndex] > i)
    74.                     {
    75.                         player2Hearts[i].SetActive(true);
    76.                     }
    77.                     else
    78.                     {
    79.                         player2Hearts[i].SetActive(false);
    80.                     }
    81.                 }
    82.                 break;
    83.             case 2:
    84.                 for (int i = 0; i < player3Hearts.Length; i++)
    85.                 {
    86.                     if (playerLife[playerIndex] > i)
    87.                     {
    88.                         player3Hearts[i].SetActive(true);
    89.                     }
    90.                     else
    91.                     {
    92.                         player3Hearts[i].SetActive(false);
    93.                     }
    94.                 }
    95.                 break;
    96.             case 3:
    97.                 for (int i = 0; i < player4Hearts.Length; i++)
    98.                 {
    99.                     if (playerLife[playerIndex] > i)
    100.                     {
    101.                         player4Hearts[i].SetActive(true);
    102.                     }
    103.                     else
    104.                     {
    105.                         player4Hearts[i].SetActive(false);
    106.                     }
    107.                 }
    108.                 break;
    109.             case 4:
    110.                 for (int i = 0; i < player5Hearts.Length; i++)
    111.                 {
    112.                     if (playerLife[playerIndex] > i)
    113.                     {
    114.                         player5Hearts[i].SetActive(true);
    115.                     }
    116.                     else
    117.                     {
    118.                         player5Hearts[i].SetActive(false);
    119.                     }
    120.                 }
    121.                 break;
    122.             case 5:
    123.                 for (int i = 0; i < player6Hearts.Length; i++)
    124.                 {
    125.                     if (playerLife[playerIndex] > i)
    126.                     {
    127.                         player6Hearts[i].SetActive(true);
    128.                     }
    129.                     else
    130.                     {
    131.                         player6Hearts[i].SetActive(false);
    132.                     }
    133.                 }
    134.                 break;
    135.             case 6:
    136.                 for (int i = 0; i < player7Hearts.Length; i++)
    137.                 {
    138.                     if (playerLife[playerIndex] > i)
    139.                     {
    140.                         player7Hearts[i].SetActive(true);
    141.                     }
    142.                     else
    143.                     {
    144.                         player7Hearts[i].SetActive(false);
    145.                     }
    146.                 }
    147.                 break;
    148.             case 7:
    149.                 for (int i = 0; i < player8Hearts.Length; i++)
    150.                 {
    151.                     if (playerLife[playerIndex] > i)
    152.                     {
    153.                         player8Hearts[i].SetActive(true);
    154.                     }
    155.                     else
    156.                     {
    157.                         player8Hearts[i].SetActive(false);
    158.                     }
    159.                 }
    160.                 break;
    161.         }
    162.     }
    163. }
    164.