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

Initally I went the Enum route as that seemed easiest and less prone to errors when setting the three colors and assumed they wouldnt change.
I also went for an [ExecuteInEditMode] so I could see the color changes in edit mode without having to press play. This worked mostly.

I then decided I wanted the designer to have more options so centralised the colors to a ColorManager and an array of colors and my ColorChanger script to have a serialized int colorNo and would call the ColorManager to get current color from said array.
I also included an OnValidate which would call the ColorManager and make sure the int was valid

I then felt a need to see the colors as you chose them for the colored objects and decided a window would be a nice way may be and certainly a learning curve.
A Brackleys tutorial vid helped with the bonus of changing multiple selected objects at the same time.
Even managed to add an undo kind of. (Only of which I was aware from one of the RPG courses - ta Sam).
Still tweaking but it works well enough for me though not helped that I introduced 3D objects into my game so that involves Materials rather than just colors…

I use various Gameobject Brushes (part of the Tilemap 2Dextras set up) to paint on my colored objects.
These show up white. I then select as many as I want and Colorize them from my window and it automatically changes the color.