SOLVED - 2D Character Selection Screen

Hi, I have made UI, buttons, sprites, and everything. But only I’m trying to make a script for character selection screen. Does anyone have script or explenation how to create it?

There are some problems to discuss. How does it should work? Do you have buttons like 1/2/3/4/5 or Previous/Next?
Actually, solution is pretty simple - you just need to loop over GameObjects and Active only specific one or just play with object pooling and index.

Example Code

[SerializeField] GameObject[] _CharacterPrefabs; // Array of your characters as GameObject
    List<GameObject> _CharacterPool = new List<GameObject>();
    int index = 0;

    void Awake()
    {
        // Make Object Pooling List with all of your characters
        int length = _CharacterPrefabs.Length;
        for (int i = 0; i < length; i++)
        {
            GameObject prefab = Instantiate(_CharacterPrefabs[i]);
            prefab.SetActive(false);
            _CharacterPool.Add(prefab);
        }
    }

    // Assign this method to your Right Button;
    public void OnRightButton()
    {
        _CharacterPool[index].SetActive(false); // Deactive actual character
     
        index++;

        // Prevent list index error
        if (index > _CharacterPool.Count - 1)
            index = 0;

        _CharacterPool[index].SetActive(true); // Active next character
    }

    // Assign this method to your Left Button;
    public void OnLeftButton()
    {
        _CharacterPool[index].SetActive(false); // Deactuve actual character

        index--;

        // Prevent list index error
        if (index < 0)
            index = _CharacterPool.Count - 1;

        _CharacterPool[index].SetActive(true); // Active previous character
    }

Something like that, didn’t test it.

1 Like

You can just create multiple prefab for each character you have , and instantiate it by click on the button wich contain the prefab you select :slight_smile:

1 Like

Well, I’m making character selection screen for 2D fighting game, and when I press button of character (the image of character on the Selection Screen) then when i press select button(shown on the image), I want it to load the Level selection Screen, then character that I choose, into the level what I choose.
It looks like this in image that i uploaded below…

3324644--258947--characterSelection.png

You can try this example (IconPrefabs and CharacterPrefabs should have the same order, for example if you want “Warrior” to be first, he should be first on IconPrefabs and CharacterPrefabs):

public class GameManager : MonoBehaviour
{
    static public int CharacterIndex = 0;
    [SerializeField] GameObject[] _CharacterPrefabs;

    void Awake()
    {
        // Instantiate selected character = make player
        Instantiate(_CharacterPrefabs[CharacterIndex]);
    }
}
public class SelectionScript : MonoBehaviour
{
    [SerializeField] Button[] _IconPrefabs;

    void Awake()
    {
        // Instantiate your icons(Buttons) with characters and add them listener to change CharacterIndex in main script.
        int length = _IconPrefabs.Length;
        for (int i = 0; i < length; i++)
        {
            int j = i;
            Button _Icon = Instantiate(_IconPrefabs[i]);
            _Icon.onClick.AddListener(()=> GameManager.CharacterIndex = j);
        }
    }
}

To achieve icon order one by one take a look at scroll view and other groups components and spawn these icons in its content.

1 Like

Well thanks, but now I have some strange warnings do you know how to fix it? Thanks.


Those can be safely ignored, so long as you fill in their values in the inspector. :slight_smile:

1 Like

Thank you methos, now it’s fixed.

Cool :slight_smile:

But now I dont know how to bring this character into new scene, any suggestions?

I think one of the easiest suggestions is to save a name or number.
Then, when you load the actual game scene use that name/number to get the correct player from (a/the) list of characters :slight_smile:

1 Like

Ah what are name and number? :slight_smile:

Don’t know what you mean

Well, like names of the player characters. or numbers (ie: their index in a list).

If the character selection is say 5 characters, that’s potentially 5 names and/or index 0 through 4.

Now, if this list of characters is also in your game scene, you just need 1 of those pieces of information to be able to take from the list in the game scene, the selected character for the user to use/play …

That make sense?

1 Like

Did you mean that i need to put prefab of sprite(character) into the hierarchy of the level i want to load or? Sorry for wasting you, just dunno nothing yet Thanks:p

Actually all you need to do in this script is make and assign prefabs => buttons with icon of character and prefabs of your character. CharacterIndex is static so it’ll save between scenes.

1 Like

Well, I am saying that is one option that sounds reasonable. :slight_smile:
Try it out. Either with a script with all of the characters in the same order you have on the selection screen or by name, whichever seems easier for you.
Then, save the chosen character name/index (PlayerPrefs or a static variable maybe), and then when you load the level, use that information to choose the correct character to display (ignoring the others).

Ah okay, sorry if CharIndex is already static, no need to make a new one. Just use that same index (same order of the list) in a script ; at the start of the level it activates the correct character (and/or deactivates every other char but the one chosen).

1 Like

Ah, I don’t know did I do well. I’ve created 2 GameObjects 1st with one 2nd with another one script, and i placed prefab and sprite on inspector, if is that what you mean. After I made a script Scenemanager.LoadScene and set it into select button… But when I click the button it loads level, without selected character. Thanks guys…

SOLVED! It was my bad. Couldn’t find my character because it was problem in sprite. Sorry about my mistake, and thanks guys, I appreciate your help.

Hey, cool, glad you got it working :slight_smile: