- Home /
Get variables from an editor script through a regular script
Is there a way to get the variables in an editor script from a regular script?
For example:
public class ExampleEditor : Editor
{
float a = 5;
}
and then in another script I get the value of a.
Thanks!
Could you expand the question? What do you mean by regular script?
Any script inside an Editor folder will be in the ""Assembly-CSharp-Editor" and other scripts will be in "Assembly-CSharp"
So you probably can't be access them outside of the editor folder unless you use reflection? Is there another way? Though this will most definitely only work in the UnityEditor - not on device etc)
Here's how to get your value 'a' in a non editor script:
using UnityEngine;
using System.Collections;
using System.Reflection;
using System;
// get all loaded assemblies
Assembly[] loadedAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();
// find the editor assembly
Assembly editorAssembly = Array.Find(loadedAssemblies, x => x.GetName().Name == "Assembly-CSharp-Editor");
// find the type within the editor assembly
Type type = Array.Find(editorAssembly.GetTypes(), x => x.Name == "ExampleEditor");
// Editor classes need to be instantiated through ScriptableObject.CreateInstance
object editorObject = ScriptableObject.CreateInstance(type);
// get the field of the value we're looking for
FieldInfo fi = type.GetField("a", BindingFlags.Instance | BindingFlags.Public);
Debug.Log(fi.GetValue(editorObject));
Your answer
Follow this Question
Related Questions
Assign Color[] var in prefabs using Editor Scripting 1 Answer
Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents) 1 Answer
Move slider as moving object moves between nodes 1 Answer
How to get notification of moving a GameObject in the hierachy when editing the scene? 1 Answer
How to assign ScriptableObject reference via Editor script? 0 Answers