- Home /
Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents)
Hey all,
I have a custom editor that I am working on and I really need access to all of the scene's GameObjects script components' variables, in the same way that we can do when we have a UnityEvent. In UnityEvents we can choose a GameObject from the scene, and then pick one of its script components and call functions or change variable values for example. I do not need to call functions or change variables, I just need to read the values of the variables.
To be more precise, I am using this for conditions for a dialogue system. I will have a boolean var which is the result of a comparison between some string, int, bool, etc. in some script of a GameObject, and then compare this with some string, int, bool, etc. E.g. I have a player script somewhere, and I need access to its speed var which is an int, and then in my editor, I can choose this variable in the same way as UnityEvents, and then I compare this to greater than >20.
I hope I was clear with my explanation of the situation.
Any help is greatly appreciated!
Answer by BastianUrbach · Sep 09, 2019 at 09:52 AM
You could use System.Reflection. Here is a simple example that logs all fields and their values of a given GameObject:
void ListFields(GameObject gameObject) {
var components = gameObject.GetComponents<Component>();
foreach (var component in components) {
var fields = component.GetType().GetFields();
foreach (var field in fields) {
Debug.Log(field.Name + ": " + field.GetValue(component));
}
}
}
Your answer
Follow this Question
Related Questions
Custom editor script for folders? 1 Answer
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Is there any way to create folders in the heirarchy to group objects? 1 Answer
Responsive Editor UI Button with custom style | How to remove GUIStyle.hover delay 0 Answers