Hello guys, sorry if its a duplicate question but i can’t figure it out!
I have a prefab with a script+custom inspector. I want to add attach a script through an EditorObjectField . Later, I’ll create instances of this prefab and attach different scripts with other methods. So, how can i make this field generic and also, how to access these methods?
Clear? :[
This way you can assign any script that inherits from MonoBehaviour to the inspector field or even make an array out of it. Remember, that you cannot attach Non-Monobehaviours to GameObjects, so all your GameObject code will be MonoBehaviour anyway.
To access functions on those scripts you will have to know the type in advance and then cast to a new instance of the correct type.
For example:
using UnityEngine;
public class ChangeImage : MonoBehaviour
{
public MonoBehaviour script;
void Start()
{
// SomeClass must be a MonoBehaviour.
// Whatever you assign to "script" must also be SomeClass.
// To make this dynamic you would have to check via if clause or switch statement.
SomeClass myClass = (SomeClass)script;
if(myClass != null)
myClass.SomeFunction();
}
}