Reputation: 313
I am making the simple game and I can not figure out how to change colors in code . I have Parent game object where i plan to attach the script to change child element colors.
For example I have this:
Parent:A
Childs in A = 1,2;
I want to get all 2 elements and change color in first child to black in second to white.
I want to change tag when i change color so I can achieve the random color to be on childs.
I don't know how i can get that 2 childs in that parent to change property.
Can i name the childs to 1 and 2 and then look from code for child with gameobject name 1 and change color property , if I can how i can do it ?
Upvotes: 1
Views: 880
Reputation: 1213
The below portion of code is a quick example usage of the GetProperty method. Simply use MyGetProperty and MySetProperty as shown below. Remember the variables that will be referenced by a string must be properties.
public class Parent {
private int child1 = 0;
private int child2 = 0;
public int iChild1 {
get {
return child1;
}
set {
child1 = value;
}
}
public int iChild2 {
get {
return child2;
}
set {
child2 = value;
}
}
public void MainMethod() {
MySetProperty("iChild1",1);
MySetProperty("iChild2",2);
string strOutput = String.Format("iChild1 = {0} iChild2 = {1}",MyGetPrperty("iChild1"), MyGetPrperty("iChild2"));
}
public object MyGetProperty(string strPropName)
{
Type myType = typeof(Parent);
PropertyInfo myPropInfo = myType.GetProperty(strPropName);
return myPropInfo.GetValue(this, null);
}
public void MySetProperty(string strPropName, object value)
{
Type myType = typeof(Parent);
PropertyInfo myPropInfo = myType.GetProperty(strPropName);
myPropInfo.SetValue(this, value, null);
}
}
Upvotes: 3