Swapping GameObjects using Arrays

Hello!

I am making my way through the Unity learn Junior Programmer course (it is amazing) and I just wanted to ask for advice to see if there is a better way to accomplish this using less code.

GOAL: Swap 20 Hat GameObjects in my Hierarchy using Arrays

This code in my GameManager script works perfectly BUT if I had say 20 different Hat GameObjects I would have 20 lines of SetActive(true); or (false); code.

public class GameManager : MonoBehaviour

{
    public Button hatButton;
    public GameObject[] hatArray;

    // Start is called before the first frame update
    void Start()
    {
     
    }

    // Update is called once per frame
    void Update()
    {

    }

    // my button code

    public void SwapHatPink()
    {
        // 1 - swap the hat to Pink
        hatArray[1].SetActive(true);
        hatArray[2].SetActive(false);
    }

    public void SwapHatBlue()
    {
        // 2 - swap the hat to Blue
        hatArray[2].SetActive(true);
        hatArray[1].SetActive(false);
    }

}

Any advice you could offer to a newbie like me would be super appreciated!
Thank you :slight_smile:

You could just store the index of the current active object, deactivate that, activate the new one and store its index again. So instead of using a specialized method for each color you would have a generic one that takes the color as parameter (preferably an enum which can be cast to int for indexing).

    public class GameManager : MonoBehaviour
     
    {
        public Button hatButton;
        public GameObject[] hatArray;
   public HatColor currentHatColor;
     
        // Start is called before the first frame update
        void Start()
        {
             SetHatColor(HatColor.Blue);   // initialize with a value
        }
     
   public void SetHatColor(HatColor newHatColor)
   {
       hatArray[(int)currentHatColor].SetActive(false);   // deactivate the old hat
       hatArray[(int)newHatColor].SetActive(true);   // activate the new one
       currentHatColor = newHatColor;       // remember the new one
   }
     
    }

public Enum HatColor
{
   Pink,
   Blue,
   Green,
};

Note that you must ensure that the enum always matches the array contents. You could also add some checks wether the index esist at all for example with Debug.Assert.
And all hat game objects should start inactive. Or you deactivate them in a separate method.

Thank you so so much for the detailed reply exiguous! I really appreciate your time and help.
I’ve been re-reading over your script and have just tried it out. I also did some googling on “enums” and I think I understand how it works!

For my buttons to work I added this at the end of the script. So am I correct in thinking I would simply just need to add a new “SwapHatColour” method for each individual button I have on my canvas to swap out the different hat colours?

public void SwapHatPink()
    {
        SetHatColour(HatColour.Pink);
    }

Then I referenced “SwapHatPink” in my button onClick event.

7596040--942286--button.JPG

This is so helpful thank you again :slight_smile:

Thats right. You can also use methods with one parameter in uGUI but as far as I remember (it’s been a while since I lasted worked with it) only with standard Unity types (Vector3, Material etc). So you could try if you can assign the general SetHatColor method directly and pass the desired enum as parameter. But as I said I’m not sure this is possible (but it’s worth a try anyway). If all else fails you just could pass the representing int to it, cast it to the enum and then just use this. But this really defeats the purpose of using enums in the first place and you are better off with your method of a specialized method for each color.

1 Like

oh thats great! im so happy that the code is working. thank you so much again for your help :slight_smile:

1 Like