Scripting the CoinSpawner Component
Our spawning Component will create a randomly selected type of coin whenever the GameObject with this script becomes active. Create a new script named CoinSpawner and fill it out as shown in Listing 8.5.
public class CoinSpawner : MonoBehaviour{ public GameObject coinSpawnPoint; public Object[] coinPrefabs; void Start() { this.SpawnCoin(); } void SpawnCoin() { int random = Random.Range(0, coinPrefabs.Length); GameObject coin = Object.Instantiate (coinPrefabs[(Random.Range(0, coinPrefabs.Length))], coinSpawnPoint.transform.position, coinSpawnPoint.transform.rotation) as GameObject; coin.rigidbody2D.AddForce(new ...
Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.