Control the Instantiated objects number

Hello everyone, I am a freshman for Unity.
I am trying to design a mini-game which need plant some thing.
There will be a game object bulid once I click mouse.
But the object will create one by one if I keep on click.
I want to use one variable to control the game object.

code here:
var aOb: Rigidbody;
var plantForce: float;
var test :int = 5;
var zz: boolean;

function Update () {
if( test >=0)
{zz = true;
}

if(Input.GetButton(“Fire1”) zz == true)
{

var newapple = Instantiate(aOb, transform.position, transform.rotation);
newapple.name = “apple”;

Physics.IgnoreCollision(transform.root.collider, newapple.collider,true);
if(newapple >= test)
zz = false;
}
//newapple.rigidbody.velocity = transform.TransformDirection(Vector3.forward*10);

// test–;

//if( test == 0)
//zz = false;
}

@script RequireComponent(AudioSource)

by the way. my english is poor, so hope that everyone can understand what I mean. :stuck_out_tongue:

What are you trying to do with this code? What is supposed to happen?

I can see a few obvious logic errors in your code. (Things your code does that do not make sense.)

if( test >=0 )
{
   zz = true;
}

You set test to equal 5 in the code and never change test again. This if() statement will always be true.

If you want test to be some kind of counter, you need to change the value. Maybe lower it by one every time you create an apple?

if(Input.GetButton("Fire1") zz == true)

The zz is always true, so that part of the if() will always be true.

if(newapple >= test)

The variable newapple is type Object. The variable test is type Int. Why are you comparing the two?

Even if the comparison would be true, the “zz = false;” will be overwritten by the very next call to Update().

I think you may be looking for something like:

var applesObject : Rigidbody;   //The object to make copies of.
var applesLeft  : int = 5;        //How many apples we can create.

function Update () 
{
   //Only create an apple if there are apples left to create AND the player pushes the Fire1 button.
   if(applesLeft > 0  Input.GetButton("Fire1"))
   {
      var newApple = Instantiate(appleObject, transform.position, transform.rotation);
      newApple.name = "apple";

      Physics.IgnoreCollision(transform.root.collider, newApple.collider, true);

      --applesLeft;  //Lower the apple remaining count by one.
   }
}

For your own sanity and the sanity of others who will look at your code, do not use variable names like “zz” and “test” that have no meaning. Programming code is so much easier to read and understand if you use variable names that mean something like “applesLeft” or “applesToMake”.