- Home /
How do I find a Prefab after I Replaced said Prefab?
I am trying to save a gameobject as a Prefab. I want to save it as a prefab as I want to place it down into a battle field. I currently can save the prefab but as soon as I do the prefab updates and becomes missing for my save script. using UnityEngine; using System.Collections; using UnityEditor; public class SaveCraft : MonoBehaviour { %|-1580967504_1|% %|-1919096758_2|% %|-303516546_3|% PlayersCraft = PrefabUtility.GetPrefabObject(PlayersCraft) as GameObject; %|98586054_5|% } }
How do I get around that problem?
Answer by alexss21 · Dec 07, 2016 at 02:22 PM
Try this:
Instantiate<GameObject>(Resources.Load("path to prefab"));
This will make a clone of the original gameobject. The prefab must be located in a map called resources, so if this is your path in the explorer:
Unity Project\Assets\Resources\Prefabs\PlayersCraft
then your path is:
"Prefabs\PlayersCraft"
PrefabUtility.ReplacePrefab(gameObject, (Resources.Load("Prefabs/PlayersCraft")), ReplacePrefabOptions.ConnectToPrefab); if anyone wants to know how I was using the code! I just call a public function every time I want to save or load the prefab!
O$$anonymous$$G THAN$$anonymous$$YOU! I replaced my code asking for the prefab and just placed it into the replace prefab code! PrefabUtility.ReplacePrefab(gameObject, (Resources.Load("Prefabs/PlayersCraft")), ReplacePrefabOptions.ConnectToPrefab); is what I used if anyones interested! @alexss21
Please take care to post comments as comments and not answers. Best,