- Home /
Assigning a script in custom editor
I'm trying to assign a script within a custom editor. Can this be done? And if not why haven't they allowed this? I've tried EditorGUI.ObjectField, PropertyField, and DragAndDrop. I keep getting this Error : Can not convert UnityEngine.Object to . I've even tried using casting and other conversion methods. Any help would be greatly appreciated. Thanks :D
Can you explain in greater detail what you are hoping to achieve. Do you simply want to add a custom $$anonymous$$onoBehaviour
component to a game object from an editor script?
You can select a script using an object field. The following page from the user guide explains how to add a component given a specific $$anonymous$$onoScript
: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoScript.GetClass.html
I have a class variable in my script. I want to be able to drag a script that derives from that class and assign it to that variable using the editor.
Let me make this clearer. I can't use EditorGUILayout.ObjectField. Cause it's a script I'm referencing. $$anonymous$$onoScript looks to be the way to go but how can I convert it to my script? I can get the type but when I try to convert it I can't. It says 'cannot convert $$anonymous$$yScript to $$anonymous$$onoScript'. The script variable I have is just a basic class, it doesn't inherit from any thing.
Afaik you can only select a script with $$anonymous$$onoScript
if it inherits from UnityEngine.Object
.
The easier way would be just to use a text field and input the name of the class (including namespace).
A slightly more involved approach would be to create a popup menu and use reflection to display a list of selectable classes. You could filter the list using a custom interface or attribute:
[SelectableIn$$anonymous$$yField]
public class $$anonymous$$yNonObjectClass {
...
}
Answer by ricardo_arango · Apr 21, 2013 at 03:56 AM
You would do something like this:
MyClass obj;
void OnGUI(){
obj= EditorGUILayout.ObjectField(obj, typeof(MyClass), true) as MyClass;
}
Note that I used EditorGUILayout.ObjectField for simplicity, the usage is similar for EditorGUI.ObjectField.
You can find more information in the documentation:
http://docs.unity3d.com/Documentation/ScriptReference/EditorGUILayout.ObjectField.html http://docs.unity3d.com/Documentation/ScriptReference/EditorGUI.ObjectField.html