Switch Between Characters

I want to have a list of characters I will be able to switch between.

So far I am able to switch to "Player2" when I am currently "Player1" (when you press 1 you are player1, when you press 2 you are player2, etc).

I am able to do this by destroying my current character (Player1) and instantiating Player2 where I am standing (for now they spawn at a fixed point, but that I can change easily).

My problem is that when I press 2 on my keyboard a SECOND time (at this point I'm already playing as Player2), the GameObject Player2 is instantiated a SECOND time. If keep on doing this, I have a bunch of clones running around.

So to sum it up:

How do I stop the function being called by button1 after it is pressed until a different number is pressed? Or should I do a sort of toggle system? Or use a boolean of some sort? I tried these alternatives, but I am bombarded with errors. I am simply a beginner..

Here's the simple version of my script:

function Update(){

if (Input.GetKeyDown(KeyCode.Alpha1)){
    print("1 pressed");     
 }

if (Input.GetKeyDown(KeyCode.Alpha2)){
    print("2 pressed");     
 }

}


Here's what I'm really trying to work with...:


var Player1 : GameObject;

var Player2 : GameObject;

function Update(){

if (Input.GetKeyDown(KeyCode.E)){
    print("E button!");

Destroy(GameObject.FindWithTag("Player1")); 
var instance1 : GameObject = Instantiate(Player2, transform.position, transform.rotation);

}

 if (Input.GetKeyDown(KeyCode.R)){
    print("R button!"); 

Destroy(GameObject.FindWithTag("Player2")); 
var instance2 : GameObject = Instantiate(Player1, transform.position, transform.rotation);  

 }

}

The easiest will probably to have a variable to tell which player is instantiated.

var currentPlayer : int = 1;

function Update()
{
    if (Input.GetKeyDown(KeyCode.Alpha1) && currentPlayer != 1 ){
        print("1 pressed");    
        currentPlayer = 1;
     }

    if (Input.GetKeyDown(KeyCode.Alpha2) && currentPlayer != 2 ){
        print("2 pressed");    
        currentPlayer = 2; 
     }
}

Other idea, have tried to use player1.active = false; player2.active = true; instead ? (It need to be more clever than that, have a currentPlayer : GameObject; etc, but you get my point)

Old discussion; but took me three days to work out “Character Switch” solution; so thought I would post my solution, in case it helps someone else. I am a beginner at coding; so it is possible my code could be more efficient; but it seems to work perfectly to switch between two characters. In my game, both players have a Main Camera and a Free Look Camera Rig. So those were some of the game objects that had to be switched. Here is the code I used…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityStandardAssets.Cameras;

public class CharacerSwitch : MonoBehaviour
{
    public GameObject player1;
    public GameObject player2;

    public GameObject cam3;
    public GameObject cam1;

    public GameObject rig1;
    public GameObject rig2;

    void Start()
    {
        player2 = GameObject.Find("Player2");
        player1 = GameObject.Find("Player1");

        cam3 = GameObject.Find("Camera3");
        cam1 = GameObject.Find("Camera1");

        rig2 = GameObject.Find("FreeLookCameraRig2");
        rig1 = GameObject.Find("FreeLookCameraRig");

        cam3.SetActive(false);
        rig2.SetActive(false);

        cam1.tag = "MainCamera";
        cam3.tag = "Camera 2";
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))    //"O" for "Other" or "Other Player" 
        {
            if (player1.GetComponent<CapsuleCollider>().enabled == true)    //collider arbitrary, but reflects object state
            {
                player1.GetComponent<Rigidbody>().isKinematic = true;
                player2.GetComponent<Rigidbody>().isKinematic = false;

                player1.GetComponent<CapsuleCollider>().enabled = false;
                player2.GetComponent<CapsuleCollider>().enabled = true;

                player1.GetComponent<ThirdPersonCharacter>().enabled = false;
                player1.GetComponent<ThirdPersonUserControl>().enabled = false;

                player2.GetComponent<ThirdPersonCharacter>().enabled = true;
                player2.GetComponent<ThirdPersonUserControl>().enabled = true;

                cam1.SetActive(false);
                rig1.SetActive(false);

                cam3.SetActive(true);
                rig2.SetActive(true);

                cam1.tag = "Camera 2";
                cam3.tag = "MainCamera";
            }
        }

        if (Input.GetKeyDown(KeyCode.P))   //"P" for "Player" 
        {
            if (player1.GetComponent<CapsuleCollider>().enabled == false)   //collider arbitrary, but reflects object state
            {
                player1.GetComponent<Rigidbody>().isKinematic = false;
                player2.GetComponent<Rigidbody>().isKinematic = true;

                player1.GetComponent<CapsuleCollider>().enabled = true;
                player2.GetComponent<CapsuleCollider>().enabled = false;

                player1.GetComponent<ThirdPersonCharacter>().enabled = true;
                player1.GetComponent<ThirdPersonUserControl>().enabled = true;

                player2.GetComponent<ThirdPersonCharacter>().enabled = false;
                player2.GetComponent<ThirdPersonUserControl>().enabled = false;

                cam1.SetActive(true);
                rig1.SetActive(true);

                cam3.tag = "Camera 2";
                cam1 = GameObject.Find("Camera1");
                cam1.tag = "MainCamera";

                cam3.SetActive(false);
                rig2.SetActive(false);
            }
        }
    }
}