- Home /
ScriptableObject and Monobehaviour
Hi, i have a class Warrior that inherits from a class Character, these warriors have to be saved on a Dictionary.
Dictionary<string,Character> characters = new Dictionary<string, Character>();
To save them i'm using:
private void createWarrior(string name){
warrior = gameObject.AddComponent<Warrior>();
warrior.setWarriorAttributes(name,1000,1,3);
characters.Add(name,warrior);
}
But with that each time a create a new Warrior a new Component is Add to a game object, and at this point i only want to save them in the dictionary. But i can't use ScriptableObject because later i have to instantiate prefabs with the script Warrior and he has to be inherits from MonoBehaviour so i can do it.
With what i replace AddComponent ? I tried use new:
Warrior = new Warrior()
It works, but unity gives me a warning, and i read that's not recommend.
Sorry for my english, google translate.
What is the question? You might wanna edit your post.
So what exactly is the format of data you need to store to a database?
Answer by pengo · Aug 01, 2014 at 06:02 AM
You cannot create a MonoBehaviour unless it is attached to a GameObject.
You could make a new class WarriorAttributes : ScriptableObject. When you are ready to instantiate a Warrior, you can set its state from WarriorAttributes. A little bit like the Memento pattern.
Alternatively, you could instantiate new GameObjects, attach the Warrior MonoBehaviour, and make it inactive until you wish to use them with gameObject.SetActive(false)
Your answer
Follow this Question
Related Questions
Best practice for many similar scripts in a game? 0 Answers
Adding inherited class to list 1 Answer
AudioClip in ScriptableObject 1 Answer
How to reference MonoBehaviour in ScriptableObject? 2 Answers