- Home /
How to assign variables to each GameObject in an array
public GameObject[] enemy;
That is all I have so far.... I want to assign the following variables to each GameObject in the array enemy, but don't know how to.
public int count; public float lowRange; public float highRange;
Answer by bakir-omarov · Jun 11, 2018 at 10:36 AM
You have to create Enemy script with this variables (public int count; public float lowRange; public float highRange;), and then in GameController or anywhere you have to find all Enemes (for example by tag), and change each attached script's values.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityAnswers_ArrayVariable : MonoBehaviour {
private GameObject[] enemies;
// Use this for initialization
void Start () {
// get all GameObjects in scene with tag Eemy
enemies = GameObject.FindGameObjectsWithTag("Enemy");
// change every value in each GameObject of our array
foreach (GameObject _enemy in enemies)
{
_enemy.GetComponent<Enemy>().count = 1;
_enemy.GetComponent<Enemy>().lowRange = 1.3f;
_enemy.GetComponent<Enemy>().highRange = 5.6f;
}
}
}
Here is what I have so far. I want it so that if I declare the public class Wave[] in another script, then in the inspector it would ask me how many waves I want, and then it would also ask in each wave, how many enemies I want. So kind of like an array within an array, that is visible in the inspector.
(also if you are wondering what the class, "EnemyProperties" is, it is another class I created with the declared variables that I mentioned earlier.)
Here is the inspector so far.........