Making Character Creation Menu - Can't Figure Out Code

I’m still a newbie at this, but I’ve spent a lot of time in the documentation today trying to figure this out. It’s time to admit I’m stuck.

I am attempting to make a character customization menu for my game. I think that part of the problem is that I’m using individual sprites for each moving part, rather than using sprites and redrawing each frame, so I have… a lot of parts. Since the mess of parts is working for me right now, I’m just going to leave well enough alone.

My problem is that the code I’ve got that’s supposed to change the skin tone when a button is clicked, isn’t working. I’m pretty sure I’ve got all the correct things hooked up in the correct places, and some (one) aspects are working (I can minimize then open the color selection menu, but it won’t start minimized).

I’ve at least gotten it so that I don’t have any errors.

I know leaving everything ‘public’ isn’t best practice. I’ll change all that when I’ve got it working.

I’m using this

tutorial from 2017, so for all I know, the only problem is something that’s been changed since then.

  1. How do I fix this mess to make the colors change properly when the button is clicked?

  2. Is there a way I can clean up the code, so the same 8 things don’t appear… everywhere?

a. I’m expecting it to get worse when I start adding hair and clothes.

  1. How do I fix the selection menu so that it only appears when it’s opened?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharacterCreationManager: MonoBehaviour
{

    public GameObject colorPanel;

    public SpriteRenderer baseHead;
    public SpriteRenderer baseBody;
    public SpriteRenderer baseLArm;
    public SpriteRenderer baseRArm;
    public SpriteRenderer baseLHand;
    public SpriteRenderer baseRHand;
    public SpriteRenderer baseLLeg;
    public SpriteRenderer baseRLeg;

    public Color skin1;
    public Color skin2;
    public Color skin3;
    public Color skin4;

    public int whatSkinColor = 1;

    void update()
    {
        SkinColor();
    }

    public void SkinColor()
    {
        if(whatSkinColor == 1)
        {
            baseHead.color = skin1;
            baseBody.color = skin1;
            baseLArm.color = skin1;
            baseRArm.color = skin1;
            baseLHand.color = skin1;
            baseRHand.color = skin1;
            baseLLeg.color = skin1;
            baseRLeg.color = skin1;
        }
        else if(whatSkinColor == 2)
        {
            baseHead.color = skin2;
            baseBody.color = skin2;
            baseLArm.color = skin2;
            baseRArm.color = skin2;
            baseLHand.color = skin2;
            baseRHand.color = skin2;
            baseLLeg.color = skin2;
            baseRLeg.color = skin2;
        }
        else if(whatSkinColor == 3)
        {
            baseHead.color = skin3;
            baseBody.color = skin3;
            baseLArm.color = skin3;
            baseRArm.color = skin3;
            baseLHand.color = skin3;
            baseRHand.color = skin3;
            baseLLeg.color = skin3;
            baseRLeg.color = skin3;
        }
        else
        {
            baseHead.color = skin4;
            baseBody.color = skin4;
            baseLArm.color = skin4;
            baseRArm.color = skin4;
            baseLHand.color = skin4;
            baseRHand.color = skin4;
            baseLLeg.color = skin4;
            baseRLeg.color = skin4;
        }
    }

    public void ChangeSkinColor1() { whatSkinColor = 1; }

    public void ChangeSkinColor2() { whatSkinColor = 2; }

    public void ChangeSkinColor3() { whatSkinColor = 3; }

    public void ChangeSkinColor4() { whatSkinColor = 4; }

    public void OpenColorPanel()
    {
         colorPanel.SetActive(true);
    }

    public void CloseColorPanel()
    {
        colorPanel.SetActive(false);
    }
}


Not sure what your exact issue is with in #1. Do you mean you don’t want skin color to change immediately when they select a color, and instead they have to press a button? If so just move the SkinColor function out of the update function and have the button call it instead. However, I don’t think that is what you mean as that seems less desirable to me.

2 you can just make them into a function themselves. Just take a skin color as an argument and set all objects need skin color changing in that function. Likewise for any other pieces that would share colors you can do the same thing. Under the hood you’re still referencing each one, but makes your code a lot cleaner.

3 you can either make the menu a prefab, and instantiate it when opened, then destroy it when closed, or you can toggle the enabled option for it and keep it always around. Either way will work. If you want to keep your menu very script minimal, the enable/disable is probably better as you can do those changes directly in the editor button options. You can always change later too if you want.