Skip to content

Instantly share code, notes, and snippets.

@OutlawGameTools
Created November 21, 2015 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OutlawGameTools/7717ee7a29a635f54058 to your computer and use it in GitHub Desktop.
Save OutlawGameTools/7717ee7a29a635f54058 to your computer and use it in GitHub Desktop.
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