- Home /
Question by
nowhereman · Oct 18, 2011 at 02:36 AM ·
colorstringconversion
Input string to Color conversion
Right now I have it set up so that the player can input a string and if it matches an default color, an object changes to that string. However, is there a way I could simplify my code and do something like (pseudocode)-
var INPUT : string = "blue";
renderer.material.color = Color.INPUT;
instead of
var INPUT : string = "blue";
if(INPUT=="blue"){
renderer.material.color = Color.blue;
}
else if(INPUT=="red"){
renderer.material.color = Color.red;
}
etc...
Comment
Best Answer
Answer by Eric5h5 · Oct 18, 2011 at 03:10 AM
Use a dictionary.
var colors = new Dictionary.<String, Color>();
colors["blue"] = Color.blue; // and so on
Then you can do
if (colors.ContainsKey(input)) {
renderer.material.color = colors[input];
}
Worked great. Just had to import System.Collections.Generic;