Collections used for prefabs

I made a little health bar and stats collection which is reused for different enemies (all the ones needing health bars).

If I make a Game Object in my project folder for my enemy I am only allowed to have components added to that Game Object …so I cannot add my health bar collection to that Game Object. So instead, I assume I have to make a collection for my enemy, so I make a new collection called Enemy and add the health bar. So that’s good. I have prefab now with a health bar. But I also want my enemy to have things other than Collections like a sprite. So I make, in my project folder, a Game Object called EnemyGo then go back to my Enemy collection and add the Game Object. Now I have a Enemy Collection with a health bar collection and a enemy Game Object in it.

This works kinda. I have a script attached to EnemyGO and when the enemy is killed it needs to delete itself. But when I do go.delete(".") it doesn’t delete the health bar. And I don’t know how to reference the collection the game object’s script is on… I don’t think collections are even a thing in runtime. So how do I delete the collection AKA how do I delete the health bar too? I tried go.delete(go.get_parent()) but that doesn’t work.

The reason I’m wanting a prefab is so that I can place many of these Enemy collections on my map and have them all work independently without having to rebuild them for each new entry.

There must be a better way of predefining a prefab…what is it?

Also, if all my enemies are collections then I cannot put them in a folder (aka, game object) in my level so the level entries cannot be organized at all…just a lot of collections.

Any help on the best way to organize and create prefabs which allows this to work would be appreciated! Keep in mind the health bar collection has a few game objects in it for sprites and such.

Thanks!

You can recursively delete children of a game object:

-- delete the game object and all children
go.delete(".", true)

It’s already setup to do that. The problem is the health bar is not a child. It’s like this:

Collection: Enemy
- Collection: Health and other things; many game objects
- GameObject: Enemy sprite, and script and other components

So when I call go.delete(".", true") from EnemyGO it deletes all of EnemyGO but not the EnemyGO.

Don’t manually place “prefabs” collections like you describe for the use case you describe. Use collectionfactory.create() which returns a list of the GOs in the collection. Then you have a manger script to manage all of this, or pass the info to the main script of the “prefab”.

You have some more options:

  • Have a GO which is a spawn point for prefabs. These you place in the scene. They’re just a GO / sprite / script. The script tells the collection factory to create a prefab at its location (and its info is managed like described above), then the temp GO destroys itself, all done on init.
  • Hard code the relative addresses of every GO component of your prefab. Addressing within a collection prefab is relative so you can message them in a relative way.
  • Build your own in engine level editor. This is in part what we do in our games. Save data in some external format like JSON then use it to generate the world on level init.
  • Use another external editor like Tiled and place entities with it. Then use the data it produces like when building your own in engine editor to generate your levels in engine at level init. Useful for some kinds of projects.
6 Likes