- Home /
Assign spells/abilities dynamically to a GameObject?
Hello,
I'm creating a game where the player should be able to pick a few skills from an array of skills, to be assigned to that player.
I'm considering what the best approach to achieve this is.
I have a skill-script that looks like this:
public class Skill : MonoBehaviour {
public Texture2D picture;
public KeyCode key;
public int cooldown;
private float timeStamp;
void Start () {
}
void Update () {
if (Input.GetKeyDown(key))
{
// Update timestamp with cooldown
timeStamp = Time.time + cooldown;
}
}
public bool OnCooldown()
{
return timeStamp > Time.time;
}
}
In turn, I have 4 instances of this script assigned to my player GameObject, which means the player has 4 skills for which I can control the properties for (cooldown etc).
My question is, how can I make this behaviour more "dynamic"? As you can see I only have a generic Skill-script at the moment, but will create a few more specific abilities, with their own script. The problm now is that I've assigned a certain set of scripts to my GameObject, but I want to be able to control what abilities that player should have on runtime.
I'm considering to send some variable down to the Skill-script that defines what type of ability it is, and then search in a list of objects for this ability and assign it's properties/functions to the generic "Skill".
If you have any ideas on how to best achieve this I would be really grateful!
I figured it out myself!
What I did was I just had my spells inherit from "Skill" and then added a skill-array for my player-object, where I used AddComponent() to dynamically add scripts of type Skill.