Instantiating a parent (prefab) with child (prefab)

I’m thinking through a solution for spawning enemies in a 2D top down shooter (looking down from y axis onto x and z). The current setup I’m trying to figure out is as following (with javascript):

  • A GameObject always present in the scene which serves as a Spawner from which the enemy instantiates from
  • This enemy is made of two prefabs:
  1. A Movement prefab with a movement script to it
  2. A Shoot prefab with a shooting script

The Movement prefab acts as a parent to the Shoot prefab, the child, so that the Shoot prefab follows the Movement prefab. Structuring it like this should allow for easy composition of sticking various Shoot prefabs onto various Movement prefabs. By assigning these prefabs as variables in the Spawner script you can specify in the Spawner script which prefabs you want to instantiate and basically copy/paste the code and fill in the variables containing the prefabs as you want to (and spawn them based on Time.time or something).

So in the end you have a box of prefabs for movement, a box of prefabs for shooting, and you are able to assemble them as you please in the Spawner’s script.

I believe you can’t really specify beforehand that these two prefabs are parent and child and have to do this in the script’s update, unfortunately (and what you possibly wouldn’t want to do either way taking the scenario as described above in regards), which probably will lead to a somewhat intricate and difficult script, i.e. more than I can comprehend on my own and from what I’ve researched about it so far.

So then, to come to my issue, is it possible to instantiate a Movement prefab from the Spawner, then instantiate a Shoot prefab (from the Spawner or new Movement prefab?) and assign that as a child to the Movement prefab? Or how would you approach this?

It’s not a matter of “please write this script”, I’d much rather understand the logic behind it first and then get to grips on how you’d construct it with javascript.

Thank you for reading through this to the end and I hope you’re able to offer me an answer!

Ps. Note that I’m not that experienced with Unity and JavaScript (as you probably already figured out, I’m just a hobbyist taking some spare time to do this). If you could take that into regards when suggesting a solution it´d be much appreciated!

hmm, “2D top down shooter… instantiation…Or how would you approach this?..”

my approach is:
A instantiation script on any gameobject in the scene that
-spawns some enemyprefabs accross the scene
-instantiates a playerPrefab which contains basically
–cameras
–the avatar
–other shenanigans for splitscreen, HUD…

the avatar(or any ship in my game) has a script called “Loadout” on it. this script adds all other components i need to get it working, done via gameobject.AddComponent<MyAwsomeScript.cs>()
-movementscripts
-weaponScripts that read trigger variables in
-TriggerScripts
-…
then i link them together by creating component variables at start, e.g.

private MyAwsomeScript mas;
private void Start(){
	mas = gameObject.AddComponent<MyAwsomeScript>();//creates the component and links it to this variable. if you already have a ths component you can use GetComponent
	
	//then you can access variables + functions that are public in this class
	mas.awsomeVariable1 = 5F;
	mas.awsomeFunction(3F);
}

this is what i do to dynamically generate my ships out of a loadout selection, you can check it out at http://marrt.elementfx.com/