- Home /
Question by
hamoo7dfuad · Feb 18 at 11:42 AM ·
c#multiplayerlistgameobjectsplayers
How to add and remove objects from a list when they get destroyed and instantiated
Hi, I have a multiplayer game when the player joins the lobby, he gets automatically added to the list but when the player gets destroyed, it shows me an error so to fix it I need to remove it from the list when it gets destroyed and the code just works when the game starts, like when somebody joins after the game it doesn't add him to the list, in other words, what I want is so to remove the object when it gets destroyed and add the object to the list when he joins, I hope that I explained my issue well.
public List<GrabController> grabScript = new List<GrabController>();
private GrabController grab;
GameObject[] player = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject onePlayer in player )
{
grab = onePlayer .GetComponent<GrabController>();
grabScript.Add(grab);
}
Comment