Search Unity

Help regarding a Lives System

Discussion in 'Editor & General Support' started by CONQUESTX20, May 13, 2021.

  1. CONQUESTX20

    CONQUESTX20

    Joined:
    Mar 24, 2020
    Posts:
    3
    Code (csharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8. public class Respawn : MonoBehaviour
    9.  
    10. {
    11.  
    12.     [SerializeField] private Transform player;
    13.  
    14.     [SerializeField] private Transform enemy;
    15.  
    16.     [SerializeField] private Transform respawnPoint;
    17.  
    18.  
    19.     void OnTriggerEnter(Collider other)
    20.  
    21.     {
    22.  
    23.         if(other.tag == "Player")
    24.  
    25.         {
    26.  
    27.             FindObjectOfType<LivesManager>().HurtP1();
    28.  
    29.         }
    30.  
    31.         player.transform.position = respawnPoint.transform.position;
    32.  
    33.  
    34.        if (other.tag == "Enemy")
    35.  
    36.         {
    37.  
    38.             FindObjectOfType<LivesManager>().HurtP2();
    39.  
    40.         }
    41.  
    42.         enemy.transform.position = respawnPoint.transform.position;
    43.  
    44.     }
    45.  
    46. }
    Code (csharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8. public class LivesManager : MonoBehaviour
    9.  
    10. {
    11.  
    12.     public GameObject player;
    13.  
    14.     public GameObject enemy;
    15.  
    16.  
    17.     public int P1Life;
    18.  
    19.     public int P2Life;
    20.  
    21.  
    22.     public GameObject Player1Wins;
    23.  
    24.     public GameObject Player2Wins;
    25.  
    26.  
    27.     public GameObject[] P1Health;
    28.  
    29.     public GameObject[] P2Health;
    30.  
    31.  
    32.     // Start is called before the first frame update
    33.  
    34.     void Start()
    35.  
    36.     {
    37.  
    38.      
    39.  
    40.     }
    41.  
    42.  
    43.     // Update is called once per frame
    44.  
    45.     void Update()
    46.  
    47.     {
    48.  
    49.         if(P1Life <= 0)
    50.  
    51.         {
    52.  
    53.             player.SetActive(false);
    54.  
    55.             Player2Wins.SetActive(true);
    56.  
    57.         }
    58.  
    59.  
    60.         if (P2Life <= 0)
    61.  
    62.         {
    63.  
    64.             enemy.SetActive(false);
    65.  
    66.             Player1Wins.SetActive(true);
    67.  
    68.         }
    69.  
    70.     }
    71.  
    72.  
    73.     public void HurtP1()
    74.  
    75.     {
    76.  
    77.         P1Life -= 1;
    78.  
    79.         for (int i = 0; 1 < P1Health.Length; i++)
    80.  
    81.         {
    82.  
    83.             if (P1Life > i)
    84.  
    85.             {
    86.  
    87.                 P1Health[i].SetActive(true);
    88.  
    89.             }
    90.  
    91.             else
    92.  
    93.                 P1Health[i].SetActive(false);
    94.  
    95.         }
    96.  
    97.     }
    98.  
    99.  
    100.     public void HurtP2()
    101.  
    102.     {
    103.  
    104.         P2Life -= 1;
    105.  
    106.         for (int i = 0; 1 < P2Health.Length; i++)
    107.  
    108.         {
    109.  
    110.             if (P2Life > i)
    111.  
    112.             {
    113.  
    114.                 P2Health[i].SetActive(true);
    115.  
    116.             }
    117.  
    118.             else
    119.  
    120.                 P2Health[i].SetActive(false);
    121.  
    122.         }
    123.  
    124.     }
    125.  
    126. }

    Ok so I am trying to implement a lives system into my game and the code I am presenting here is my current scripts, the idea is that there is a death zone in the Unity Project that is meant to replicate the bottomless pits. The Code here is meant to get rid of some icons I had to indict lives but when player 1 falls off the map they don’t respawn unlike player 2 which seems to be functional. For Reference Player 1 is given the Player Tag while Player 2 gets the Enemy Tag.

    This is the error I got

    IndexOutOfRangeException: Index was outside the bounds of the array.

    LivesManager.HurtP1 () (at Assets/Scripts/LivesManager.cs:51)

    Respawn.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Respawn.cs:15)

    I tried looking up the error on Youtube and I couldn’t find a solution that worked for me, I have tried it out with Player 1 using the Enemy Tag and that seems to function perfectly so I was wondering if there is a good way to fix this error, like which lines of code actually needs to be changed ETC.


    Any help on this would be deeply appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Look carefully at the two
    for()
    loops above.

    You have written:

    See the continue condition? You wrote
    1 < P2Health.Length


    That will always evaluate true as long as you have 2 or more items in that array.

    That means i++ will endlessly march higher until it blows out the end of the array.

    What you want is actually
    i < P2Health.Length


    :)
     
    Last edited: May 13, 2021