Basically I’ve created a prefab, “badGuy” of which there are three types. Each has it’s their own sprite images. So I created the following enum in a script attached to my badGuy prefab:
public enum BadGuyType {
green,
blue,
red
}
All along I have been using one image for my prefab since I didn’t have a enum. The badGuy game object had a public property for me to add it through the inspector and instantiate it a number of times as below:
public GameObject badGuy; //I set this in the inspector
void Start () {
badGuys= new List<GameObject> ();
int numberOfBadGuys = 6;
Camera camera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
for (int i = 1; i < numberOfBadGuys + 1; i++) {
GameObject badGuyObject = (GameObject)Instantiate(badGuy, new Vector3(Screen.width*i/2, Screen.height*i/6, camera.nearClipPlane ), Quaternion.identity );
badGuys.Add(badGuyObject);
}
How can I set images (preferably programmatically) for my BadGuyType enum and select them when instantiating my badGuy game objects?
If you want each badGuy to have its own properties then you need to create a general class for badguys and put all the variables inside it. Create a Script called BadGuysManager then implement all your bad guy's actions inside there or copy the one from below.
Function signatures:
In your
test.cs
script, you can add the BadGuysManager script with a list.The code below should be in your
BadGuysManager
.cs script. This should get you started. You can easily extend or add more functions to theBadGuysManager
class.