Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[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;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    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
Posted
Updated 12-May-21 14:38pm

1 solution

C#
for (int i = 0; <big>1</big> < P1Health.Length; i++)


I think you want to use "i" for the compare. (in more than one place - copy and paste)
 
Share this answer
 
Comments
Patrice T 12-May-21 21:21pm    
+5 good catch

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900