Created
July 17, 2017 16:16
-
-
Save JonathanYin/98dbe5a943917f15617fc2a87896356c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class BackgroundChanger : MonoBehaviour | |
{ | |
public GameObject bg1; | |
public GameObject bg2; | |
public GameObject bg3; | |
public GameObject bg4; | |
public GameObject bg5; | |
void OnGUI() | |
{ | |
if (GUI.Button(new Rect(60, 75, 20, 20), ":)")) | |
{ | |
if (bg1.activeSelf && bg2.activeSelf == false && bg3.activeSelf == false && bg4.activeSelf == false && bg5.activeSelf == false) | |
{ | |
//Debug.Log("first bg is on"); | |
bg1.gameObject.SetActive(false); | |
bg2.gameObject.SetActive(true); | |
} | |
else if (bg1.activeSelf == false && bg2.activeSelf && bg3.activeSelf == false && bg4.activeSelf == false && bg5.activeSelf == false) | |
{ | |
//Debug.Log("second bg is on"); | |
bg2.gameObject.SetActive(false); | |
bg3.gameObject.SetActive(true); | |
} | |
else if (bg1.activeSelf == false && bg2.activeSelf == false && bg3.activeSelf && bg4.activeSelf == false && bg5.activeSelf == false) | |
{ | |
//Debug.Log("third bg is on"); | |
bg3.gameObject.SetActive(false); | |
bg4.gameObject.SetActive(true); | |
} | |
else if (bg1.activeSelf == false && bg2.activeSelf == false && bg3.activeSelf == false && bg4.activeSelf && bg5.activeSelf == false) | |
{ | |
//Debug.Log("fourth bg is on"); | |
bg4.gameObject.SetActive(false); | |
bg5.gameObject.SetActive(true); | |
} | |
else if (bg1.activeSelf == false && bg2.activeSelf == false && bg3.activeSelf == false && bg4.activeSelf == false && bg5.activeSelf) | |
{ | |
//Debug.Log("fourth bg is on"); | |
bg5.gameObject.SetActive(false); | |
bg1.gameObject.SetActive(true); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script creates a GUI button on the screen which allows the player to change the background image. Using a series of if-statements, I was able to have the button go through all the images chronologically, before looping back to the first.