- Home /
Question by
OfficeThrashing · Sep 17, 2018 at 04:54 AM ·
c#stringparametersoop
How to send Color.black as a string but it will act like code. See description for more.
I want to send String as a Parameter. String will contain the code. when it will pass and when it comes to execution it will be converted into code.
like
void Start()
{
COLORMATC("Color.black");
}
void COLORMATC(Color c)
{
Debug.Log(c)
}
Something like that. Is it possible?
Comment
Best Answer
Answer by Hellium · Sep 17, 2018 at 05:13 AM
The best you can AFAIK is to give "black" as parameter and use reflection:
// Put this at the top of your script
using System.Reflection;
public void ColorMatC( string c )
{
Type type = typeof(Color);
var property = type.GetProperty(c, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if( property == null )
throw new System.ArgumentException("No property named " + c + " exist in the Color type");
Color color = (Color) property.GetValue(null);
Debug.Log( color ) ;
}