Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:16
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 JonathanYin/98dbe5a943917f15617fc2a87896356c to your computer and use it in GitHub Desktop.
Save JonathanYin/98dbe5a943917f15617fc2a87896356c to your computer and use it in GitHub Desktop.
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);
}
}
}
}
@JonathanYin
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment