- Home /
Acces monobehaviour script from editor script
Hey,
A bit new to editor scripts, i'm using a EditorWindow script made by the community to replace gameobjects in the scene with a prefab, but i'm trying to modify the script so that i can get and set some values of some of the scripts on these gameobjects. The first thing i noticed is that there is no way to access these monobehaviour scripts from the EditorWindow script, is there a way around that?
Think of editor scripts as UnityEngine + UnityEditor. So basically you have full access to EVERYTHING PLUS THE EDITOR. To access a script from one of your gameobjects, you simply need to do something like this.
$$anonymous$$yScriptType myScript;
if (myScript = go.GetComponent<$$anonymous$$yScriptType>())
{
myScript.someValue = someNewValue;
}
There's really hundreds of ways to gain a reference to an existing or new script, you just use the same references you would normally. Even Resources.Load() works for editor scripts. You can easily choose between editor code and runtime code to make whatever you want to happen. Even the EditorGUILayout and EditorGUI is completely interchangeable with GUILayout and GUI classes.
Point is, don't be afraid to use NON-editor code in your editor script.
Let's say the script i'm trying to access is called $$anonymous$$yScript.cs, in the EditorWindow script when i type $$anonymous$$yScript it doesn't find it, it doesn't seem like i have access to any of the monobehaviour scripts in my project, did i understand correctly?
If your script is within a namespace, you need to include that namespace in your editor class in a using statement. Otherwise the IDE won't recognize it. As well, if you are trying to access monobehaviours, be sure to include the using UnityEngine;
namespace. By default if you create a script in Editor it will assign a namespace based on the folder, like namespace Assets.Scripts
which is easily changeable, but you won't be able to reference classes inside a namespace without using the namespace.
If you're using Visual Studios, it will show you the hierarchy on the right hand side, there is one tricky thing to be aware of. If your scripts are in the Plugins Hierarchy, then they themselves will not have access to NON-plugins, but all NON-plugins can access the Plugins. $$anonymous$$oving a script from Plugins, Editor, or the main project, you can just grab it from the hierarchy and drag and drop it into the hierarchy you want it in. $$anonymous$$eep in $$anonymous$$d, this will not change the namespace, and occasionally has the effect of duplicating a class file.
Answer by Martin_Gonzalez · Mar 26, 2018 at 07:16 PM
Update to Answer
Scripts under Plugins folder are in Assembly-CSharp-Editor-firstpass.
Change the script to Assets/Editor/
Assembly-CSharp-Editor-firstpass and Assembly-CSharp-firstpass are compiled first, so they cannot know anything about other Assemblies classes
$$anonymous$$yScript can't be found, in $$anonymous$$onoDevelop it's just red, it's as if the scripts in an editor folder can't see any other scripts outside the folder, so i'm trying to figure out a way around that
Can you show me an image of your project hierarchy?
It's pretty standard, Assets/Scripts for my regular scripts, Assets/Plugins/Editor for my editor scripts
Answer by NoMoneys · Mar 26, 2018 at 06:10 PM
For accesing a class on a gameobject, you should have a reference to it in some variable. Or atleast have a reference to the gameobject you're trying to get the class from.
Example:
Classname ourClass = (variable that holds gameobject).GetComponent();
then use the dot(.) operator to acces public fields and properties within that class.
I have a reference to the gameobject but i can't access the class, gameobject.GetComponent() doesn't work, i just can't seem to be able to access any monobehaviour scripts from an EditorWindow script
There is no error, i just can't access any of my classes from an EditorWindow script, when i type the name of the classes it just doesn't exist
Your answer
Follow this Question
Related Questions
Saving a variable from an editor script into something like a gamemanager script? 0 Answers
Dynamic content in custom EditorWindow 1 Answer
Weird bug when dragging object onto EditorGUILayout.ObjectField 0 Answers
EditorGUILayout.LabelField not centered properly 1 Answer
Is there a way to get all editable fields of a component in a script? 2 Answers