# Balloons *Sourced assets* - Balloons instantiate automatically. - When the player pops them on click, the balloon drops a connected fruit. - When fruits collide, they stack and scale up. ![[DESIGNS/TOYS/Balloons.gif]] ### Balloon Controller ```cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BalloonSceneController : MonoBehaviour { public GameObject Balloon; public List<GameObject> childList; public GameObject victoryPanel; private int xPos; private int childIndex; private void InstantiateBalloons() { ChooseParameters(); GameObject balloonClone = Instantiate(Balloon, new Vector3(xPos, 2, 0), Quaternion.Euler(0,0,0)); GameObject childClone = Instantiate(childList[childIndex], new Vector3(xPos, -3, 0), Quaternion.Euler(0, 0, 0)); childClone.transform.parent = balloonClone.transform; balloonClone.GetComponent<Rigidbody2D>().velocity = new Vector3 (0, 5, 0); Destroy(balloonClone, 5); } private void ChooseParameters() { childIndex = Random.Range(0, childList.Count); xPos = Random.Range(-18, 18); } private void InstantiateRepeat() { InvokeRepeating("InstantiateBalloons", 0, 2); } private void Victory() { if (BalloonUI.poppedCountIndex >= 10) { Debug.Log("Victory!"); victoryPanel.SetActive(true); CancelInvoke("InstantiateBalloons"); } } private void Start() { InstantiateRepeat(); } private void Update() { Victory(); } } ``` ### FruitController - When identical objects collide ```cs public class BalloonFruitController : MonoBehaviour { private void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == gameObject.tag) { if (gameObject.GetInstanceID() < col.gameObject.GetInstanceID()) { gameObject.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f); Destroy(col.gameObject); } } } } ``` ### Balloon Exploder ```cs public class BalloonExploder : MonoBehaviour { // Option: // One manager, // Sound from prefab (it's not most effective if each object has sound) // Sound would be an audio clip // Balloons from prefab //public AudioSource snapSound; private Animator balloonAnimator; private string animationBool = "Explodes"; private GameObject audioController; private void Start() { balloonAnimator = GetComponent<Animator>(); audioController = GameObject.Find("AudioController"); } private void OnMouseDown() { Debug.Log("Clicked!"); balloonAnimator.SetBool(animationBool, true); //snapSound.Play(); BalloonAudio balloonAudioScript = audioController.GetComponent<BalloonAudio>(); balloonAudioScript.PlaySound(); gameObject.GetComponent<CapsuleCollider2D>().enabled = false; BalloonUI.poppedCountIndex++; if (gameObject.GetComponent<Transform>().childCount > 0) { Transform child0 = gameObject.transform.GetChild(0); child0.transform.parent = null; child0.AddComponent<Rigidbody2D>(); } } } ``` ### BalloonSound ```cs public class BalloonAudio : MonoBehaviour { // Should be an array for several sounds public GameObject sound0; public void PlaySound() { GameObject sound0Clone = Instantiate(sound0); Destroy(sound0Clone, 2f); } } ```