I’m writing an editor script which needs to save values to a text file, and I need the path of that script so that I can save the text file in the same folder.
I have tried the following with no luck:
private Object thisScript;
private string thePath;
void OnEnable ()
thisScript = FindObjectOfType(typeof(myScript)) as myScript;
thePath = AssetDatabase.GetAssetPath( thisScript ); // Returns null
thePath = AssetDatabase.GetAssetPath( this ); // Returns null as well.
Debug.Log("thePath = " + thePath);
Now Unity already shows me the path of this script in the Debug window, when the Debug.Log() appears, telling me where the Log came from, so it knows where it is, but I can’t access it…I feel like a complete idiot right now!
Thanks for your time guys, any help will be very appreciated as always!
You tried to get an asset path from an instance of a normal .NET / Mono class which of course doesn’t work The class / type that is represented by a MonoScript is not an asset, it’s just a mono class. You need to use MonoScript.FromScriptableObject to get the MonoScript asset that contains your desired type. Note that the types Editor (for custom inspectors) and EditorWindow are derived from ScriptableObject.
To get the MonoScript of a “normal” MonoBehaviour class, you would use MonoScript.FromMonoBehaviour
As final hint: The type MonoScript is derived from TextAsset, so your script is just a text file from Unity’s point of view. However when the MonoScript asset is compiled, you can get the System.Type object for the class that is represented by this script with MonoScript.GetClass.
Hi I’m trying to do the same thing … Did you get this to work? What was the code you ended up using?