Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
For the "Many Game Objects from One Prefab and an Array of Sprites" tutorial. http://masteringunity2d.com/unity-2d-tutorial/many-game-objects-from-one-prefab-and-an-array-of-sprites/
using UnityEngine;
using System.Collections;
public class SpawnAnimal : MonoBehaviour {
public GameObject animalPrefab;
public Sprite[] animalSprites;
public void MakeRandomAnimal()
{
int arrayIdx = Random.Range (0, animalSprites.Length);
Sprite animalSprite = animalSprites[arrayIdx];
string animalName = animalSprite.name;
GameObject newAnimal = Instantiate (animalPrefab);
newAnimal.name = animalName;
newAnimal.GetComponent<Animal>().animalName = animalName;
newAnimal.GetComponent<SpriteRenderer>().sprite = animalSprite;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.