- Home /
Prefabs double at each instance?
Hey, I'm making a 2d game, which requires 6 prefabs which spawn enemies at specific intervals. But when there are some instances already in the game, when the prefab instantiates again, it makes 2 of the prefabs spawn, and on the third time 3 will spawn and so on. I have all of the prefabs above the screen, out of view. Here is the code on my prefabs: var Enemy:Transform; private var Timer:float;
function Awake() {
Timer = Time.time + 5;
}
function Update() {
if (Timer < Time.time) {
Instantiate(Enemy, new Vector3(-2.567029, -1.864899, -4.613899), transform.rotation);
Timer = Time.time + 5;
}
This is the code that spawns the enemies at certain times. Is there a way to stop the doubling up when they spawn? It means my points system is screwed up! Cheers
This script will make an Enemy at the exact same spot every 5 seconds. That's not what you want?
Yeah I do, it spawns the first enemy fine, but when another 5 seconds pass, two spawn ins$$anonymous$$d of one.
You don't have this script attached to the enemy do you? Each object with this script attached will make a new enemy every 5 seconds.
Answer by flaviusxvii · Feb 16, 2013 at 06:10 PM
Don't put this script on your "Enemy" prefabs. Attach this to an empty game object somewhere in the scene, then it'll spawn one enemy every 5 seconds.
Remember to upvote/accept answers! And re$$anonymous$$d others to do so!
Just tested it, works perfectly, thank you. $$anonymous$$arked as answer
Hey flaviusxvii, I had the same problem and your solution fixed it, thanks heaps! :D. I was hoping you can explain why that happens?
Your answer
Follow this Question
Related Questions
Set a Prefab's variable when you instantiate it, but before it calls Awake? 2 Answers
Instantiate a Prefab thats been passed through 2 scripts? 2 Answers
Unity Frame Drop From Instantiating Prefab that has script 0 Answers
Instantiate a prefab with a key press to a relative location? 1 Answer
Change Variable on Another Script 2 Answers