Instantly share code, notes, and snippets.

What would you like to do?
For the "Many Game Objects from One Prefab and an Array of Sprites" tutorial.
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 =;
GameObject newAnimal = Instantiate (animalPrefab); = 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