switch between two players

hi need help with switching between two players what want is to be able to switch between two prefabs instantly with the press of a key I want it to destroy the current prefab and spawn the other prefab in the same position of the prefab that was destroyed so basically what I want is to be able to switch back and forth between prefabs

You don’t want to destroy your prefab. Especially if you want to give the user control over it. What you want to do is just activate and deactivate them. Why not have two prefabs grouped under the same parent game object. Have all your controlling scripts on the parent and then just use SetActive to enable and disable them.

If its just always going to be two players. Something like:


public bool IsPlayerOneActive = true;

public GameObject PlayerOne;

public GameObject PlayerTwo;

void Update()
{
    if(Input.GetKeyDown(KeyCode.Space))
    {
        IsPlayerOne = !IsPlayerOne;
        PlayerOne.SetActive(IsPlayerOneActive);
        PlayerTwo.SetActive(!IsPlayerOneActive);
    }
}

Note: in the configuration above you will want to set player two as inactive in the inspector or in one of the early running lifecycle methods by default.

If it’s ever going to be more a slightly more sophisticated method would probably be useful. But either way it’s never going to be particularly complex and always going to be pretty much the same process.