Attaching method to scriptableobject
Hey I'm writing equipment system with each item being a scriptableObject and having a callback method attached to call it when item is used (returning bool). What solution should be used in such case? I thought about something like eventSystem but I want to reference scripts not gameobjects.
Answer by Hellium · Sep 23, 2018 at 11:20 AM
// In your ScriptableObject script
public event System.Action OnUsed;
public void Use()
{
// Do something
if( OnUsed != null ) OnUsed() ;
}
// In your other scripts which need to know when your item is used
private void Start()
{
// Supposing you have a reference to the ScriptableObject called item
item.OnUsed += OnItemUsed ;
}
private void OnItemUsed()
{
Debug.Log( "The item has been used" ) ;
}
Thanks thats great but is there any way of attaching the event in inspector? Trying to find most designer friendly solution (working in a $$anonymous$$m).
Because scriptable objects are project assets, you won't be able to "attach functions" of objects in the scene. The only solution I see is to have the following:
// In your other scripts which need to know when your item is used
public UnityEngine.Events.UnityEvent OnItemUsed ;
private void Start()
{
// Supposing you have a reference to the ScriptableObject called item
item.OnUsed += OnItemUsed.Invoke ;
}
Your answer
Follow this Question
Related Questions
How to send event to my canvas via script 0 Answers
Serializable Class On ScriptableObject Null when loaded from AssetBundle 2 Answers
How do I make a item system with ScriptableObjects where different items can have durability? 1 Answer
how do i reference to a text objects text in scripting? 2 Answers
My Scriptable Objects keep losing data due to a parsing error. How can I avoid this? 1 Answer