- Home /
How to reference a script asset
Hello everyone,
I am trying to get a reference to a script in my assetsdirectory from the inspector.
NOT a already instanced script component in the scene!
What I tried was using the Object type as a SerializeField.
[SerializeField]
Object checkable;
This was the result:
However this is too much of a choice for users and I need the selection window to only filter certain scripts that inherit from either a MonoBehaviour script or best would be from an Interface. It definitley should exclude Folders, Materials, Meshes etc...
In the end it should look like this:
Answer by JusSumGuy · May 19, 2019 at 01:20 AM
You can make a empty gameObject to hold your script. Then when you need it you can access it through the empty game object.
That's one possible way I already thought about.
But I will only do this if there is no other way around.
Because:
When using the selection the user will also see all the other GameObjects and then there is a risk of the user choosing a wrong Prefab and thus creating bugs.
Answer by user75641 · May 19, 2019 at 02:19 PM
i mean maybe you could get a public array of scripts for the user to chose and then just use an index of the array
other than that than idk unless you like use tags or something
This would require me to manage some hard references to the scripts in the resources and I'd like to definitley avoid this essential complexity.
Answer by dythervin · Feb 01 at 12:08 PM
Use UnityEditor.MonoScript, however, will work in the editor only.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201642im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
missing (transform) vs none: how to code this or query for it? 1 Answer
Why does my ExecuteInEditMode script lose its initial inspector value? 1 Answer
Arrays/Lists in the inspector of Editor Extension scripts 0 Answers
There is no GameObject attached to this GameObject 2 Answers
EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer