(Solved)For each (GameObject x in x): Error when 1 of them is destroyed

Hello,
I have a problem with my player online checker that use For each:
When a player disconnect, it return false
What can i do to keep it working when there is destroyed gameobject please?

My simplified script:

 public GameObject[] PlayerOnline;

   foreach (GameObject r in PlayerOnline)
            {
                if (r.GetComponent<PlayerManageScript>().team == 1)
                {
                    team1int += 1;
                }

            else
            { team1int = 0; }
                if (r.GetComponent<PlayerManageScript>().team == 2)
                {
                    team2int += 1;
                }
                else
            { team2int = 0; }

edit: it says Missing Gamobject on inspector after a disconect

use a for loop and check for null, if true do whatever is needed

You should really ensure you remove the gameobject from the array. If it is dynamic like that, a list may be better.

You could work around it by doing this though -

foreach (GameObject r in PlayerOnline)
{
    if(r == null)
        continue;
       
    if (r.GetComponent<PlayerManageScript>().team == 1)
    {
        team1int += 1;
    }
    else
    {
        team1int = 0;
    }
    if (r.GetComponent<PlayerManageScript>().team == 2)
    {
        team2int += 1;
    }
    else
    {
        team2int = 0;
    }
}

Although, why are you setting the ints to 0? If the last player is on team 2, then team1int will be set to 0 or vice versa.

Im setting int to zero because when no one of player is in team 1 for exemple, at same time im cheking with some int are zero, if a team got no player(so team1int = 0), a bool turn true and i use it for many things

I don’t know how list works, im afraid to remake all the script in complicated way, im gonna check on google this night, if you got advise i will take them, thank you

They are almost identical to arrays only you can easily add and remove items without copying the array yourself.

Got some problem with List:
I was adding all player who joined to the array with the tag Player, how can i do the same with List?

public bool clearPlayer = false;
     PlayerOnline = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject r in PlayerOnline)
            {
            if(r == null)
            {
                if (clearPlayer == false)
                {
                    PlayerOnline = null;
                    clearPlayer = true;
                    StartCoroutine(ClearPlayerCo(1f));
                }
            }
            if(clearPlayer == false)
                if (r.GetComponent<PlayerManageScript>().team == 1)
                {
                    team1int += 1;
                }

            else
            { team1int = 0; }
                if (r.GetComponent<PlayerManageScript>().team == 2)
                {
                    team2int += 1;
                }
                else
            { team2int = 0; }


    public IEnumerator ClearPlayerCo(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        clearPlayer = false;
    }

The fix: i clear the PlayerOnline if an array return null, with the help of a bool and a coroutine it works!

If you get an array(from the Find or w/e) you can say .ToList() (and vise versa with a list you can do .ToArray() )

How often is the function running? You don’t want to be doing a string lookup each frame.

Why not just have a list of PlayerManageScript and have each player add or remove themselves from that list when they are join/leave the game? Then you wont have to use getcomponenteach iteration either which will save some cpu usuage.