- Home /
Dynamic skill system with scriptable objects
I'm trying to make a dynamic skill system in which you can easily create new skills from the editor.
[CreateAssetMenu(fileName = "NewProjectileSkill", menuName = "ScriptableObjects/New Projectile Skill")]
public class ProjectileSkill : Skill
{
public GameObject prefab;
public float speed;
public float damage;
public float cooldown;
public float energyCost;
}
public class Skill : ScriptableObject
{
public bool enabled;
public string skillName;
}
Currently what I'm trying to achieve is to expose another field in the editor to which I can assign a function (created in a separate script) which would have atleast two parameters. That function later on will be called when the said skill collides with another object.
My question here is how can I smartly achieve this?
Answer by pietro0games · Jun 09, 2021 at 12:50 AM
You "can't" achieve this. If you create any field that is a script class of your own, you will notice that dragging a script into it won't work. You can only drag assets to fields, to create an asset of a script is needed to create a prefab with a GameObject. Using a Monobehavior prefab inside the ScriptableObject looses almost all the reasons of using a ScriptableObjec. I really wanted to achieve this too
Answer by eneIr · Jun 09, 2021 at 02:39 AM
Actually you can do it, but you will have to use MonoBehaviour script. You can still use both MonoBehaviour and ScriptableObject script together, with ScriptableObject to store other data and MonoBehaviour script to store the function with parameters. Add the code below to your MonoBehaviour script and in the editor you can drag and drop a public void() function in and you can access that function if you want, just like when you add onClick() function to an UI button. Hope that fits what you need!
using System;
using UnityEngine;
using UnityEngine.Events;
public class YourScript : MonoBehaviour
{
[Serializable]
public class YourFunction : UnityEvent { }
[SerializeField]
private YourFunction yFunction = new YourFunction();
public YourFunction Y_Function { get { return yFunction; } set { yFunction = value; } }
}