Player Switcher Code Example


using UnityEngine;

public class PlayerSwitcher : MonoBehaviour
{
    public GameObject player1;  // Assign your first player in the Inspector
    public GameObject player2;  // Assign your second player in the Inspector

    private GameObject activePlayer;

    void Start()
    {
        // Set the initial active player
        activePlayer = player1;
        SetActivePlayer(activePlayer);
    }

    void Update()
    {
        // Check if "P" key is pressed
        if (Input.GetKeyDown(KeyCode.P))
        {
            // Switch players
            activePlayer = (activePlayer == player1) ? player2 : player1;
            SetActivePlayer(activePlayer);
        }
    }

    private void SetActivePlayer(GameObject playerToActivate)
    {
        // Activate the selected player and deactivate the other
        player1.SetActive(playerToActivate == player1);
        player2.SetActive(playerToActivate == player2);
    }
}