- Home /
Spawning in unity 2d
I am making a 2d game in which you are a circle and are being chased by cubes. When these cubes collide with each other they are destroyed. I want to spawn them every so often, but I can't get them to do so properly. If I use the cube in the scene in the script, it is inevitably destroyed it stops spawning. If I turn the player into a prefab, it will go to where the prefab says it is and not the actual players current position. Any ideas?
Answer by SteenPetersen · Feb 02, 2019 at 09:58 AM
do you want to spawn the cubes occasionally or the player? im confused?
if it's the enemies you wish to spawn then I would use a Script to keep track of the enimes and spawn one on a timer:
Instantiate(myEnemyPrefab, spawnPosition.Transform.position, Quaternion.identity);
when they die I would then NOT destroy them but simply diable them and add them to an array of enemies. This way you dont have to keep destroying and instantiating enemies which can be quite taxing if you have a lot of enemies. If this is what you need let me know and ill provide an answer that shows how I have done this before.
note I said spawnPosition.Transform.position so that you can udnerstand that you can just put a transform somewhere in the map and just spawn them from that position.
You could of course just say:
[serializeField] Transform spawnPosition;
And then simply spawn them with:
Instantiate(myEnemyPrefab, spawnPosition.position, Quaternion.identity);
I converted this to an answer, please matk it as answer if it answered your question
Your answer

Follow this Question
Related Questions
newbie 2d simple sprite rotation 1 Answer
Simple Jump Script Help 1 Answer
Glitching between box colliders. One is kinematic... Help! 1 Answer
Simple Topdown Movement Problem 1 Answer