Create a prefab of custom type
Hey there! I'm trying to create a prefab from an object of type "Skill":
UnityEditor.PrefabUtility.CreatePrefab("Assets/Resources/prefabs/skills/" + skills[i].name + ".prefab", skills[i].GetComponent<Skill>());
Which gives me an error "can't convert "Skill" type to "Object"".
That variant is working well:
UnityEditor.PrefabUtility.CreatePrefab("Assets/Resources/prefabs/skills/" + skills[i].name + ".prefab", skills[i].gameObject);
But I need that prefab to Instantiate other Skill-type objects, and
Skill prefab = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Resources/prefabs/skills/" + skillNames[i] + ".prefab", typeof(Skill));
gives me an error obviously, because it can't convert a gameObject-type Prefab into a Skill-type one.
Is there any way I can make a Skill-type prefab?
Thanks!
Answer by UnityCoach · Apr 15, 2017 at 08:42 AM
A Prefab is nothing but a serialised gameObject. While it allows you to keep instances of a game object up to date in the editor, at runtime, it's nothing but a gameObject you can instantiate. Notice that PrefabUtility is part of UnityEditor assembly, thus unavailable at runtime.
The PrefabType is an Editor enum to differentiate an imported 3D model from a user created prefab, and a Prefab Instance from a Prefab.
If what you want is to store data to be used by a MonoBehaviour, I would recommend using a ScriptableObject. It allows you to create different data types, to store several data of a same type and load them at runtime.
Your answer
Follow this Question
Related Questions
How can I in-script create gameobject from prefab? 2 Answers
Correctly smooth crouch script? 2 Answers
Material.SetFloat not updating. 0 Answers
hide object with mouse enter 0 Answers
UI Text: Words at end of line jumping to next line 0 Answers