[code]<pre>using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Respawn : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform enemy;
[SerializeField] private Transform respawnPoint;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
FindObjectOfType<LivesManager>().HurtP1();
}
player.transform.position = respawnPoint.transform.position;
if (other.tag == "Enemy")
{
FindObjectOfType<LivesManager>().HurtP2();
}
enemy.transform.position = respawnPoint.transform.position;
}
}
[/code]
[code]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LivesManager : MonoBehaviour
{
public GameObject player;
public GameObject enemy;
public int P1Life;
public int P2Life;
public GameObject Player1Wins;
public GameObject Player2Wins;
public GameObject[] P1Health;
public GameObject[] P2Health;
void Start()
{
}
void Update()
{
if(P1Life <= 0)
{
player.SetActive(false);
Player2Wins.SetActive(true);
}
if (P2Life <= 0)
{
enemy.SetActive(false);
Player1Wins.SetActive(true);
}
}
public void HurtP1()
{
P1Life -= 1;
for (int i = 0; 1 < P1Health.Length; i++)
{
if (P1Life > i)
{
P1Health[i].SetActive(true);
}
else
P1Health[i].SetActive(false);
}
}
public void HurtP2()
{
P2Life -= 1;
for (int i = 0; 1 < P2Health.Length; i++)
{
if (P2Life > i)
{
P2Health[i].SetActive(true);
}
else
P2Health[i].SetActive(false);
}
}
}
[/code]
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
What I have tried:
>I have tried changing the tags - Enemy Tag works but Player Tag doesn't
>I have tried to reference the parent script into the Child Script and it doesn't function