Limit the creation of objects

It is required to limit creation of objects through Instantiate. The code creates at once two clones of object.

Excuse for my English.

Am not sure to understand the question :confused:
From what I understand, you try to instantiate one object, and two are instantiated instead, and of course you want to fix that. Is that it ?

if( Input.GetButtonDown( "Fire2" ) )
{
Instantiate (projectile, Vector3(1, 0, 1), Quaternion.identity);
}

Yes, create two copies. Need one.

You’re running the script twice. Instantiate does not return two copies.

–Eric

yea, look at your objects, and see if you didn’t place the script on another object, sometimes I do that.

There can be you me have not understood…

function Update() {
	Instantiate (projectile, Vector3(i, 0, 0), new Quaternion(0,0,0,0));
}

How at start to create one copy, instead of infinite set?

I dont understand, and having Instantiate in the update function like that is going to spawn one over and over.

What are you trying to do?

I try to cause one object.

Perhaps the problem is because you use GetButtonDown and not ButtonUp. I’m not sure, but I thought I read about another user having a problem similar to this before, and this was the solution.

There’s no issue with GetButtonDown; it won’t cause duplicates.

–Eric

Even if the example code.

Please, help me. :frowning:

From what you are saying you want something along the line of.

instance : GameObject = null;
function Update()
{
if(instance != null)
{
instance = Instantiate (projectile, Vector3(i, 0, 0), new Quaternion(0,0,0,0));
}
}

I create array and depending on value in it (0, 1) the block to be put or is not put.

var projectile : GameObject;
var n = 32;
var i = 1;
var A = new Array (n);

function Update()
{	
	A[5] = 1;
	
	for (i = 1; i <= n; i++) 
	{
		if (A[i] == 1)
		{
			Instantiate (projectile, Vector3(i, 0, 0), Quaternion.identity);
			break;
		}
	}
}

Errors:
ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
32
System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message)
System.Collections.ArrayList.get_Item (Int32 index)
UnityScript.Lang.Array.get_Item (Int32 index)
NewBehaviourScript.Update () (at Assets/NewBehaviourScript.js:18)

Arrays start at 0, so A[32] is out of range. A 32 object array would start at A[0] and end at A[31] Change your for loop to:
for (i = 0; i < n; i++)

All the same without break in the editor sticks buttons start and pause. Blocks too are created infinite set of times.

MetaPsychosis is right I think. Using down means that if the mouse button is held down even for a split second it will keep creating them until it’s not down anymore. If you use up, then it will only create them after you’ve released the mouse button

For instance, if you click on a link on a website you’ll notice if you hold the mousebutton down, nothing happens. The link doesn’t activate until you release the mousebutton.

That’s what GetMouseButton does.