- Home /
Adding behaviors to a game object at run time via scriptable object
I'm currently working on a turn-based strategy game (think Fire Emblem or Advance Wars). I have a Unit prefab that pulls its stats, sprite, etc from a scriptable object asset when it is instantiated.
Changing stats is easy, but I'm not sure how to go about giving different units unique behaviors. Say I want to design a unit that can build things. I can write a builder script, but how would I go about attaching that to the correct unit at runtime?
Is it possible for my scriptable object data holder to hold an array of scripts that get attached to the unit when it is instantiated? Is there a better way of going about this? Thanks.
Answer by iJuan · May 12, 2018 at 02:26 AM
List<MonoBehaviour> behaviours;
GameObject unitPrefab;
public void CreateUnit(int behaviour) {
GameObject instance = Instantiate(unitPrefab);
instance.AddComponent<behaviours[behaviour]>();
}
Do you mean something like that?
Ah, a list of $$anonymous$$onoBehaviors. Seems obvious in hindsight. Thank you. :)
Your answer
Follow this Question
Related Questions
Linking prefabs 0 Answers
How to set a default prefab on a C# script? 1 Answer
Prefabs Transforms LookAt 0 Answers
Unity not Instantiating as Prefab 1 Answer
Instantiate a prefab vs create one dynamically at runtime? 1 Answer