Let’s say I have 5 different enemy prefabs. I won’t know until runtime which prefab I want to be spawning.
I know that normally I can make a public GameObject
variable that I can link to a script.
What’s the best way to link the prefab programmatically to accomplish this? It doesn’t seem very efficient to have multiple variables for each prefab that I have to write boiler plate code to handle each :).
Here is the easiest way to keep track of the enemies in your scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public static List<Enemy> allEnemies = new List<Enemy>();
// Use this for initialization
void Start () {
allEnemies.Add(this);
}
// Update is called once per frame
void Update () {
}
}
I would just create a public List<GameObject>
and assign them in the editor. Then you could use a loop on the list to write the code you need to handle each prefab.