code not working help plz
I'm back with another problem. I love the results I get from here thanks everyone. I'm trying to create an array in a script with all my enemies then call it in another one, but I keep getting an error.
public GameObject[] enemychoose; //this is my array
public GameObject Selectenemy; //I want this to hold whatever enemy the array gets
public float countenemy = enemychoose.GetLength; //this checks the length of the array
void Start () {
float i = Random.Range (0, countenemy);// this it picks a random item in the list
Selectenemy = enemychoose [i]; //here Selectenemy gets the enemy chose so I can call it
}
for line 12 it says cant convert float to int;
can someone help me fix it?
Answer by LordRed30 · Jun 14, 2017 at 08:16 PM
I'm brand new to unity, but have you tried...
public static int ToInt32 ( float value }
I've used this to convert my floats recently. Sorry if it doesn't help. still learning myself.
Answer by Jawchewa · Jun 14, 2017 at 10:56 PM
If I were you, I would do a few things to improve your code. I would get rid of your countenemy variable, and just reference the length of the array directly. Also, I would replace the GetLength function with the Length property on the array, as I don't think that GetLength works the way that you think it does. Finally, I would change it to an int, so you don't have issues converting from float to int.
Basically, change your start to this:
void Start()
{
int i = Random.Range(0, enemychoose.Length);
Selectenemy = enemychoose[i];
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612124204im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why Collider2D size Y changes affects child object position 0 Answers
Why is my cube not following the other the way i want ? 1 Answer
How to Undo a lot of created objects at once? [Solved] 1 Answer
How to make hundrenths of seconds in C#? 1 Answer
Sprite renderers not rendering on Instatiated objects 2d 0 Answers