hey im trying to get my enemies to spawn a certain distance away from the player, the problem i am facing is i cant seem to fix the error with my function spawnenemy here is the code
function spawnenemy (enemies[])
{
if (enemies[0])
{
offset = new Vector3(0,Random.Range(-offsetY,offsetY),0);
Instantiate(enemies[0],new Vector3(newPosition.x,playerY+offset.y,0),transform.rotation);
}
}
and this is the for loop that iterates the array i found this when searching how to spawn enemies
for ( var i :int = 0 ; i <= enemyTypeAllowed; i++)
{
spawnenemy( enemies[i-1] );
}
hmmm but i declared enemies like this var enemies:GameObject[] = new GameObject [10]; so if i was to follow what you done would i put function spawnenemy (enemies:enemies[])
No, it’s the type. You want enemies:GameObject[ ].
However, with that added bit of context, this is starting to feel redundant. Do you even need to pass that as a parameter? Why not just use the array you have in the class already? (I am guessing this is on the same object) More relevantly, if you DO for whatever reason need to pass it as a parameter, you should name the parameter something besides enemies, since otherwise it will be ambiguous which one you’re referring to (and might give you a compile error).
edit: a deeper reading of your code: you don’t WANT to pass an array. You want to pass an object in the array, which is just an object. Remove everything from your function definition that has , and your code will (probably) work.
Also, in your for loop, you use enemies[i-1], which will immediately (when i is 0) give you an error. Use enemies instead.