I’m creating spawning enemies. So I have a prefab and then I instantiate them. My problem is when they become a prefab, the gameobjects that I’ve attached to them through the inspector gets removed and I cannot reattach the gameobjects from the hierarchy. How can I reattach the gameobjects back into the prefab?
I know that prefabs can be only attached to other prefabs. But by it’s breaking my functionality, where its not working the same way, if I were to have them both as a non-prefab object.
I’m doing some guesswork here, but I assume you have ‘public’ variables that you’ve dragged and drop in the editor (to reference other behaviors/scripts) but that’s not working, so instead in the your prefab Awake/Start/OnEnable you’ll want to add code to set those references, like
MyClass myClass = GetComponent<MyClass>();
or something like that
What you talkin’ bout Willis? (joke for old people like me - Google different strokes for reference).
A prefab just a GameObject dragged onto the Assets folder, if you add a component in the inspector then just drag the item in the hierarchy from the hierarchy back over the prefab.
The same goes for the any gameobject you add to the prefab you put in the hierarchy. Once you add something to prefab you need to update that prefab so it includes that item.
It is possible to add components when you instantiate them though. If you have 1 prefab and you want to instantiate it 3 times and each time give it a different script that’s possible. Say you instantiate the object as newEnemy then you can do this and you want three different attack scripts on them you can do this:
newEnemy.AddComponent<Attack1>();
// then on the next one
newEnemy.AddComponent<Attack2>();
// and on the final one
newEnemy.AddComponet<Attack3>();
Sorry if I missed your point or confused you with the joke :¬)