public class first: MonoBehaviour {
static public void one(){
int numb = 10;
// something
}
static public void two(){
int helth = 20;
// something
}
}
…
and i have another script like this :
…
public class secound: MonoBehaviour {
Update(){
//first.one(); // normal use .. but...
}
}
…
i want to set name of function in inspector like “tow” or “one” then that function execute in Update() in secound class …
and i want after i set the name of function… the variables inside that function showing in inspector …
so… how do i do this ?
The way to do this would be to have a Dictionary of string and method/delegate. Below is an example you need to make your own, but the idea is there.
public class First:MonoBehaviour{
public string method; // Set in inspector asthe name of the method
Dictionary<string, Action> dict = new Dictionary<string, Action>();
void Start(){
dict.Add("one",new Action(One);
dict.Add("two",new Action(Two);
if(dict.Contains(method)) // Make sure the string is in the dictionary
dict[method](); // call the method
}
}
Best would be to use a enum with the same principle:
public enum MethodName{
One, Two
}
public class First:MonoBehaviour{
public MethodName method; // Set in inspector as the name of the method
Dictionary<MethodName, Action> dict = new Dictionary<MethodName, Action>();
void Start(){
dict.Add(MethodName.One,new Action(One);
dict.Add(MethodName.Two,new Action(Two);
dict[method](); // call the method
}
}
This way, no chance that the string is wrongly written.