Character Selection Question

Hi All,

I have an issue where I have three characters in a game that are cycled through. I would actually like to put them into a UI system so the player can select which player while in game. Would anyone be able to assist with this? I haven’t been able to find any way to do this. I found some tutorials on how to do this in 3d with Play Maker, but this is a 2D game and PlayMaker doesn’t seem to be something I need to be able to do it. Code below.

public GameObject[] characters;
    public GameObject[] cameras;
    int currentActive = 0;


    void Update()
    {

        if (Input.GetKeyDown(KeyCode.O))
        {
            if (currentActive == characters.Length - 1)
            {
                currentActive = 0;
            }
            else
            {
                currentActive++;
            }


            for (int i = 0; i < characters.Length; i++)
            {

                if (i == currentActive)
                {

                    characters[i].SetActive(true);
                    if (currentActive == 0)
                    {
                        characters[i].transform.position = characters[characters.Length - 1].transform.position;
                    }
                    else
                    {
                        characters[i].transform.position = characters[i - 1].transform.position;
                    }

                }

                else characters[i].SetActive(false);


            }

            for (int i = 0; i < cameras.Length; i++)
            {

                if (i == currentActive)
                {

                    cameras[i].SetActive(true);
                    if (currentActive == 0)
                    {
                        cameras[i].transform.position = cameras[cameras.Length - 1].transform.position;
                    }
                    else
                    {
                        cameras[i].transform.position = cameras[i - 1].transform.position;
                    }

                }

                else cameras[i].SetActive(false);


            }
        }
    }

here is a tutorial on how to create buttons

just put your code on the buttons

Hey thanks for the reply. The issue is the code cycles through the characters, and it doesn’t let you choose them. I’ve followed that tutorial before. I’m looking for advice on how best to modify the code I have or come up with completely new alternative code to be able to have the character selection be done through some UI, so you can pick and choose, not just cycle through.

well to choose which character you want you just need to do:

currentActive = 0; // for character 0
currentActive = 1; // for character 1
currentActive = 2; // for character 2

and remove this part from your code:

if (currentActive == characters.Length - 1)
            {
                currentActive = 0;
            }
            else
            {
                currentActive++;
            }

Ok, so I’ve modified my code, and added the character selects to buttons, but I have an issue where I need the characters to be setactive at the last position of the previous character. Have any thoughts on this?

New Code:

public GameObject characterSwitchUI;

    public GameObject Knight;
    public GameObject Viking;
    public GameObject Pike;

    public GameObject KnightCam;
    public GameObject VikingCam;
    public GameObject PikeCam;

   

    void Update()
    {


        if (Input.GetKeyDown(KeyCode.O))
        {
            characterSwitchUI.SetActive(true);
            Time.timeScale = 0.2f;
        }
        else if (Input.GetKeyUp(KeyCode.O))
        {
            characterSwitchUI.SetActive(false);
            Time.timeScale = 1f;
        }
      

    }

    public void KnightActive()
    {
    
            Knight.SetActive(true);
            KnightCam.SetActive(true);
            Viking.SetActive(false);
            VikingCam.SetActive(false);
            Pike.SetActive(false);
            PikeCam.SetActive(false);
       
    }

    public void VikingActive()
    {
       
            Knight.SetActive(false);
            KnightCam.SetActive(false);
            Viking.SetActive(true);
            VikingCam.SetActive(true);
            Pike.SetActive(false);
            PikeCam.SetActive(false);
       
    }

    public void PikeActive()
    {
       
            Knight.SetActive(false);
            KnightCam.SetActive(false);
            Viking.SetActive(false);
            VikingCam.SetActive(false);
            Pike.SetActive(true);
            PikeCam.SetActive(true);
       
    }

you need an extra field

 public GameObject CurrentChar;

and then

    public void KnightActive()
    {
  
            Knight.SetActive(true);
            KnightCam.SetActive(true);
            Viking.SetActive(false);
            VikingCam.SetActive(false);
            Pike.SetActive(false);
            PikeCam.SetActive(false);

Knight.transform.position = CurrentChar.transform.position;
CurrentChar = Knight;
      
    }
    public void VikingActive()
    {
      
            Knight.SetActive(false);
            KnightCam.SetActive(false);
            Viking.SetActive(true);
            VikingCam.SetActive(true);
            Pike.SetActive(false);
            PikeCam.SetActive(false);
      
Viking.transform.position = CurrentChar.transform.position;
CurrentChar = Viking;
       
    }
    public void PikeActive()
    {
      
            Knight.SetActive(false);
            KnightCam.SetActive(false);
            Viking.SetActive(false);
            VikingCam.SetActive(false);
            Pike.SetActive(true);
            PikeCam.SetActive(true);
      
Pike.transform.position = CurrentChar.transform.position;
CurrentChar = Pike;
       
    }

Thanks man. Works!

1 Like