I have two scripts trying to work together. One script spawns bubbles (BubbleSpawnerS), the other script destroys said bubbles when they are left clicked (DestroyOnLeftClick).
I want to have a maximum of 10 bubbles on the screen at any given time (see MaxBubbles variable), and this works great until I get to the point where I have to bubbles on the screen.
While the bubbles are initially spawning (one at a time at random time intervals), I can left click on them to destroy them and the game will keep spawning bubbles indefinitely until I hit a total of 10 bubbles (My bubble counter static variable TOTBUB goes up by 1 each time the BubbleSpawnerS script spawns a bubble and goes down by 1 each time DestroyOnLeftClick script destroys a bubble).
When I hit 10 bubbles for the first time though, the script stops spawning new bubbles even when I click a bubble and take the TOTBUB variable down to a number less than 10. (for some reason the game breaks down and does not behave like it does during the initial spawning of bubbles, before it hits the aximum amount of bubbles of 10.)
I need the game to spawn another bubble after I destroy one of the 10 bubbles on screen, and always do this up to the maximum of 10 bubbles. How do I accomplish this?
Here are the following scripts in their entirety:
asked May 25 '10 at 08:15 PM
One of your problems is that you're assigning the result of Instantiate to the 'bubble' variable, which means that 'bubble' is no longer pointing to your prefab, but on of your instances instead. If that instance gets destroyed, no more instances will be made (because the reference will be null).
Also, there's no need for a "Respawn" function. You just need your inital Spawn function to keep ticking over indefinitely, constantly checking whether it needs to make a new bubble.
It could look something like this instead:
answered May 25 '10 at 08:27 PM