Having a problem with maintaining a spawn of 10 objects on my screen.

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:

BubbleSpawnerS


static var TOTBUB = 0;
var bubble : Transform;
var MaxBubbles = 10;
var MinTime = .1;
var MaxTime = .8;
var MinX = 0;
var MaxX = 39;
var MinY = 0;
var MaxY = 14;
function Start ()
{
    Spawn();
}
    function Spawn ()
        {
            if (TOTBUB

<pre>`            var bubble = Instantiate(bubble, Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0), Quaternion.identity);//spawn a new bubble
            TOTBUB++;
            ReSpawn();
        }
    }
function ReSpawn()
    {

        if (TOTBUB!=MaxBubbles)
         {
          Spawn();
         }

    }

`

DestroyOnLeftClick

`

var buck : int = 0; function OnMouseOver () { if (Input.GetMouseButtonDown(0)) { buck+= transform.localScale.x; Destroy (gameObject); ScoreBoard.SCORE = ScoreBoard.SCORE + buck; BubbleSpawnerS.TOTBUB=BubbleSpawnerS.TOTBUB-1; } }

`

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:

static var TOTBUB = 0;
var bubble : Transform;
var MaxBubbles = 10;
var MinTime = .1;
var MaxTime = .8;
var MinX = 0;
var MaxX = 39;
var MinY = 0;
var MaxY = 14;

function Start ()
{
    while (true) {
        if (TOTBUB < MaxBubbles) {
            yield WaitForSeconds(Random.Range(MinTime, MaxTime))
            var randomPos = Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0);
            Instantiate(bubble, randomPos, Quaternion.identity);
            TOTBUB++;
        }
        yield;
    }
}