problem with array :( pls help

var enemyLv1:GameObject;
var health:int;
var maxTime:float;
var numProduce:int;

private var readyProduce:boolean;
private var slot = new Array(numProduce);
private var timer:float;


function Start()
{
	timer = 0.0;
	readyProduce = false;
}

function Update () 
{
	
	if(readyProduce == true)
	{
		Produce();
	}
	else
	{
		timer +=Time.deltaTime;
		if(timer >= maxTime)
		{
			readyProduce = true;
		}
	}
}

function OnCollisionEnter(object:Collision)
{
	if(object.gameObject.tag=="pdamager" || object.gameObject.name == "3rd Person Controller")
	{
		Debug.Log(health);
		health -=10;
		Debug.Log(health);
		if(object.gameObject.tag=="pdamager")
		{
			var currentDamager:GameObject = object.gameObject;
			Destroy(currentDamager);
		}
	}
}

function Produce()
{
	for(var i = 0;i<numProduce;i++)
	{
		if(slot[i+1] == null)
		{
			slot[i+1] = Instantiate (enemyLv1,gameObject.transform.position,gameObject.transform.rotation);
		}
	}
	readyProduce = false;
}

this object supposed to create another gameobject called “enemyLv1” and i dont want the number of “enemyLv1” its produce become more than the number of “numProduce”

somehow this object doesnt create anyhting and i keep getting error “ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count” on this line

		if(slot[i+1] == null)

i dont know what ive done wrong with my array plsss help me :frowning:

i have attached my gameobject already and assign a number for everything already…

thx :).

why do you use i+1, use i instead.

i had tried with only i before but didnt work … array that starts from 1 looks easier for me thats the reason …

well with i+1 you would definitely overflow; you need to loop to numProduce-1. for(var i = 0;i<numProduce-1;i++)
You should really use i though.
You can also change your array like:
declare built in array

private var slot : GameObject[];

initialize the array

function Start()
{
     slot = new GameObject[numProduce];
     ...

wow after i moved my array to start() … its working now … thx ivkoni