How to Spawn a random prefab Several specific Spawn Locations?

Hi there,
I’ve just started learning c# and this problem has me stuck!

I have lots of game objects on screen at once and several different types of each object, each can be destroyed by clicking them, I need them to re-spawn exactly where it was shortly after being destroyed.

Also id like them to re-spawn in waves, so lets say there are 30 objects on screen, once 25% have been destroyed, the timer begins for a Mass re-spawn of any destroyed objects bringing the number of on screen objects back to 30.

I’ve a mental wall with this part of my project and cannot get around it, any suggestions would be appreciated.

I have lots of game objects on screen at once and several different types of each object, each can be destroyed by clicking them, I need them to re-spawn exactly where it was shortly after being destroyed.

Then I’d think you shouldn’t destroy them at all – just deactivate them (or deactivate their render component, which leaves them active but invisible). Then you can easily reactivate them (or their renderers) exactly where they were, because they’re still there.

Also id like them to re-spawn in waves, so lets say there are 30 objects on screen, once 25% have been destroyed, the timer begins for a Mass re-spawn of any destroyed objects bringing the number of on screen objects back to 30.

Well, you’ll need some sort of “Manager” object, perhaps with all these objects as children in the scene hierarchy. That’s where you’ll put a script to keep track of the time, and reactivate objects when the time is right.

Thanks Joe, things seem clearer now and I’m going to have another attempt today. Although I think they will have to be destroyed instead of deactivated because they may need to re spawn as a different type of object. For instance I have lots of different colours of cubes, if a red one is destroyed I need a random coloured one to re spawn not necessarily the red one again.

thanks anyway!

If you really need to destroy the object rather than disable it I offer two suggestions:

Either keep inactive versions of all the object types at each location, and randomly activate one whenever you need to.

Second- Keep an array of all the positions, and maybe a second one, with the same index structure, where you keep track of whether or not that object is currently destroyed.
Then every time you need to respawn you simply check if the object is destroyed or not, and simply grab the position from the position array, with the same index as the in the “isDestroyed” array, and spawn a new object there.

anyways, just my two cents - Good luck! :slight_smile: