- Home /
Using arrays and for loop with GetComponent error
{
var Enemy : int[];
var dusman : int[,];
var Probab : int[];
for (var i = 1; i< 6 ; i++)
{
for (var j = 1; j< 6 ; j++)
{
dusman[i,j] = Enemy[i].GetComponent(EnemyScript).Probab[j];
}
}
This is the code I use for defining spawn probabilities for enemies. I got 5 prefabs of enemy and all have "Probab1","Probab2" ... to 5. I want to assign those numbers to "dusman" so for example it will be like dusman12 1 means first prefab and 2 means Probab2. I use 5 different probability variables for 5 different time/level.
But I got this error when I try to compile :
'GetComponent' is not a member of 'Object'
I also assigned 5 prefab as collider to player object which includes these codes. Which defined as Enemy1,Enemy2,Enemy3.
Can anaybody help me where is the problem ? Thank You..
Just to be clear,
EnemyScript is a script on the Array of objects Enemy[i] and you are trying to access a variable Array Probab[j] on EnemyScript?
You might have to use the foreach loop to iterate each field of the array as you go.
Answer by -hiTo- · Sep 01, 2013 at 02:36 PM
You define your Enemy[] as an integer at the beginning of your script. You cannot use GetComponent on an int.
It needs to be a GameObject.
And I'm not totally sure what you're trying to accomplish. You want to spawn random enemies?
From what I gather, you would want to do something like (sorry, can only do it in c#)
public GameObject[] Enemies;
public Vector2[] Probability; //With a high and a low number
public GameObject GetEnemyToSpawn()
{
Random rnd = new Random();
int percent = rnd.Range(0, 101);
for (int i = 0; i < Probability.Length; i++)
{
if (percent <= Probability[i].x && percent >= Probability[i].y) // If percentage is within the specified range
{
return Enemies[i]
}
}
}
Your answer
Follow this Question
Related Questions
Assign Default For Large Array 1 Answer
'name' is not a member of Object (js) 1 Answer
"Object reference not set to an instance of an object" Error 1 Answer
Getcomponent Javascript error 3 Answers