- Home /
Finding a component of an editor-defined type and setting an editor-defined value
I want to be able to change a value of a component that will be defined by the level designer in the editor. The idea is to allow the designer to change the float value of any given script, wether it be a character controller, a rigidbody or an animator component.
public enum TypeOfConsequence {Activate, Deactivate, ChangeValue};
public Component ComponentName;
public GameObject Target
public string ValueName;
public float NewValue;
void PseudoCode() {
if (consequence == TypeOfConsequence.ChangeValue) {
Target.GetComponent<FindObjectOfType(ComponentName)>().ValueName = NewValue;
}
}
Any help is welcome.
Answer by Diet-Chugg · Apr 01, 2015 at 04:59 PM
Technically you can't change the value directly... But you can call a method on that object. Pass a value into the method and update the value that way.
How I did it: First off I made two classes, A LogIt class that is a component and CallComponentMethod class that is a component.
Place both scripts on a gameObject. Drag the LogIt component onto the CallComponentMethod's component field. Set Method Name to Log and Method String to any string you want logged to the console.
click on the Context Menu (Little Cog on the same line as your script name) and select " Call em' " and your function will call. With some tweaking to this code you can change the parameters sent and technically you can get a result from it too. Have not messed with it that far though.
The Cog I was speaking of is.....................................V Here V
using System.Reflection;
public class CallComponentMethod : MonoBehaviour
{
public Component component;
public string methodName;
public string methodStringParameter;
[ContextMenu("Call Em'")]
public void Call()
{
Assembly assembly = typeof(TestCaller).Assembly;
Type type = component.GetType();
Debug.Log(type);
MethodInfo methodInfo = type.GetMethod(methodName);
ParameterInfo[] parameters = methodInfo.GetParameters();
if (parameters.Length == 0)
{
methodInfo.Invoke(component, null);
}
else
{
object[] parametersArray = new object[] { methodStringParameter };
methodInfo.Invoke(component, parametersArray);
}
}
}
public class LogIt : MonoBehaviour
{
public void Log(string message)
{
Debug.Log(message);
}
}
You would have to make functions to edit your different values. A bunch of SetVariableName(value);
You would have to change this line:
object[] parametersArray = new object[] { methodStringParameter };
to send different parameters (Mine only sends a string but you could easily put in an int, float, custom class ect.). I'm only sending a string parameter here. However I think this will be enough to get you going to solve the specifics of your problem. Note that the CallComponentMethod must be using System.Reflections in order to work.
Thanks for your answer, I'll dive into it as soon as I have some time!
I gave it a try, it's definitely in the right direction. It would need some adaptation to get the functionality I want out of it and I don't have time right now to adapt it and post it here, so I'll mark your answer as "accepted". Thanks for your help :)