CLONING COLORS QUEST: ‘Light 'em Up’ - Solutions

Quest: Cloning Colors Quest
Challenge: Light 'em Up

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

This is my solution for the first challenge.
I created an enum with 3 option, and in the Start method I created a switch case that check which option was selected

using UnityEngine;

public class ColorChanger : MonoBehaviour
{
    private SpriteRenderer mySpriteRenderer;
    private Color colors;
    private enum colorChangeEnum
    {
        red, yellow, blue
    }
    [SerializeField] private colorChangeEnum color;
    void Awake()
    {
        mySpriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void Start()
    {
        switch (color)
        {
            case colorChangeEnum.red:
                colors = Color.red;
                break;
            case colorChangeEnum.blue:
                colors = Color.blue;
                break;
            case colorChangeEnum.yellow:
                colors = Color.yellow;
                break;
        }
        mySpriteRenderer.color = colors;
    }
}
2 Likes

My code was very similar. Although for some reason I was having trouble getting my enum to show up in the Inspector, even if it was a Serialized field.

First i created an array of possible colors. Those are referenced by int from other gameObjects. And can be set inside the inspector with a range slider.

    private SpriteRenderer _mySpriteRenderer;

    [SerializeField] private Color[] availableColor = new[] {Color.blue, Color.red, Color.yellow};
    [SerializeField][Range(0,2)] private int chosenColor;

    public Color[] GetAvailableColor => availableColor;
    public int SetChosenColor { set => chosenColor = value; }

void Awake()
{
    _mySpriteRenderer = GetComponent<SpriteRenderer>();
}

Set the Renderer to the chosenColor in start.

    private void Start()
    {
        if(_mySpriteRenderer != null)
            _mySpriteRenderer.color = availableColor[chosenColor];
    }
2 Likes

How did you have your enum defined? If outside of the class, it should be declared public:

public enum MyColor
{
    Red,
    Yellow,
    Blue
}
1 Like

I did get the enum working eventually but I can’t remember how. :slight_smile: I thought I had it declared as public all along, but maybe not. It’s been a while since I did this quest. Thanks for the feedback though!

My solution was very much alike. @mystic_mill you can make a variable with the type of the enum clase. Makes it so you can SerializeField it. public works to i guess tho lol

enum ColorToBe { red, blue, yellow}

[SerializeField] ColorToBe color;
1 Like