Enum help required

Hi, I am looking at enums and have created a simple project to help with learning.

My scene consists of a cube and a enum_Manager which controls the material colour on the cube.

I want to change the material colour of my cube based on the colour selection.

On the Enum_Manager I am using a switch statement to alter the material colour.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enum_Test : MonoBehaviour {

    public enum Mat_Colour{Red, Green, Blue}

    public Mat_Colour current_Colour;

    GameObject cube;

    // Use this for initialization
    void Start () {
        cube = GameObject.FindGameObjectWithTag ("Cube");
    }

    void Update(){
        Material_Switch ();
    }

    public void Material_Switch(){

        switch (current_Colour) {

        case Mat_Colour.Red:
            cube.GetComponent<Renderer> ().material.color = Color.red;
            Debug.Log ("Red");
            break;
       
        case Mat_Colour.Green:
            cube.GetComponent<Renderer> ().material.color = Color.green;
            Debug.Log ("Green");
            break;

        case Mat_Colour.Blue:
            cube.GetComponent<Renderer> ().material.color = Color.blue;
            Debug.Log("Blue");
            break;

        default:
            Debug.Log ("No Choice");
            break;
        }
    }
}

I was wondering, instead of having to manually directly tell the renderer what value to give the colour, ie typing Color.red…can i instead pull the colour value straight from the enum drop down and assign it to the material…Color.current_selection…

This I think would be more useful and flexible in future.

Any thoughts appreciated.

Hugh.

Well…I wouldn’t put this in update. If just doing for testing, that’s fine, however if you’re doing this in live, you should only check to change the color whenever you make some selection.

Depending on how you set things up, you may be able to pull the color off of something. For example if I have colored boxes and I click on the red box, I can pull it’s color and apply it to my cube, or there is the option of setting up a variable in another script that might have a color value in it that you can pull. Otherwise, nothing wrong with what you have done with a switch statement either, but I could understand that getting long if you had a ton of colors to choose from.

Enums are just a handy thing, so you get more readable code… It’s always easier to understand something like

if(selectedColor == EnumColors.Red) { ... }

than just

if(selectedColor == 1) { ... }

With the Enum you can directly see that color and know what to do, instead of searching for that number 1 to know what to do next when this condition is true.

So you’ve already done it correctly. The problem here is, that you’re only able to set custom “int” values for an enum, but no strings or other types. You could put that switch case into a static method and name it “ParseColor” (or whatever you want) and use the enum as parameter, returning a Color. That way you can use it everywhere and only have to alter that function if something changes, but this might also getting very big on a lot of different colors.

Hi Brathnann, thanks for the suggestion. I agree regarding the update, wasteful. It was purely for testing sake in this instance.

I’ll consider this a little more.

I’m wondering if I can pull the ‘color’ value perhaps as a string and append it to the Color.xxx statement.

Thanks again, I’ll continue on this.

Hugh.

Hi Sly. Thanks for the explanation and suggestion.

I’ll consider this.

Hugh.

Thanks for the help folks. The more I play with this, the more options I am considering.

I’m not quite familiar with parse.

I am wondering, if I do take a value from the Enum, then convert it to a lowercase string. Can I then directly apply it using

‘Color.string_Value_Taken.’

Is there a correct way to format this. The above code I’m guessing is a way towards this but was generating errors.

I’m getting a little away from the original point, but this is interesting to know.

Thanks,
Hugh.

(ps. can I convert directly to a lower case string or must I use ToString() and then ToLower())?

Yeah, actually that won’t work, because it’s a structure, not an enum.

Try this

1 Like