I am currently trying to create an asset from a Script that inherits a ScriptableObject.
I want to use MonoScript.GetClass() to get the class implemented by an object with the class MonoScript. But the method always returns “null”.
Is it a bug in the Indie version or is it not implemented?
I’ve created this editor window to test GetType and it works as expected:
// TestWindow.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
public class TestWindow : EditorWindow
{
[MenuItem("Tools/TestWindow")]
static void Init()
{
GetWindow<TestWindow>();
}
MonoScript m_Script;
void Test(MonoScript aScript)
{
if (aScript == null )
throw new System.NullReferenceException("No Script selected");
var type = aScript.GetClass();
if (type == null)
throw new System.NullReferenceException("The script " + aScript.name + " doesn't have a type");
Debug.Log("The classname of script "+aScript.name+" is '"+ type.Name+"'");
}
void OnGUI ()
{
m_Script = (MonoScript)EditorGUILayout.ObjectField(m_Script, typeof(MonoScript), false);
if (GUILayout.Button("Test"))
{
Test(m_Script);
}
}
}
The only way to make GetClass to return null is when i select a script file where the class name doesn’t match the filename. ScriptableObjects have the same requirement as MonoBehaviour scripts. The classname has to match the filename.