- Home /
How can I attach a gameobject to a prefab in code?
I read through and understand PrefabUtility, but I couldnt find a way to add a game object to an instantiated prefab.
The final goal is to save numbers to the editor so I can access them at a later time.
Comment
Best Answer
Answer by GaRzY · Dec 26, 2013 at 11:07 AM
GameObject prefabInstance = PrefabUtility.InstantiatePrefab(Resources.Load<GameObject>("myPrefab")) as GameObject;
GameObject otherObject = ....; //the object that you want to add to the prefab
otherObject.transform.parent = prefabInstance.transform;
PrefabUtility.ReplacePrefab(prefabInstance, PrefabUtility.GetPrefabParent(prefabInstance), ReplacePrefabOptions.ConnectToPrefab);
with the "PrefabUtility.ReplacePrefab..." line, you are simulating the "Apply changes to original prefab" button (Apply button). So If you don't want this, remove this line.