- Home /
My GUI IS Bugging Out. Can Anyone Help?
I have a series of GUI buttons at play. Each button has a texture attached to it. When it is selected the texture changes to another texture. The button labelled "Structure" automatically toggles itself off. Also only one gui.window rect pops up. The one labelled map. I cant tell what I did to get it not to work anymore. Can somebody please look at the code, bring it into unity. and identify the issue please? I tried to label it as clearly as possible
using UnityEngine;
using System.Collections;
public class GUI_Render : MonoBehaviour {
// ------------------------------------TEXTURE DECLARATION---------------------------------
public Texture2D moneyHolder;
public Texture2D pause;
public Texture2D pause_on;
public Texture2D structures;
public Texture2D structures_on;
public Texture2D upgrade;
public Texture2D upgrade_on;
public Texture2D map;
public Texture2D map_on;
public Texture2D energyELabel;
public Texture2D energyEmpty;
public Texture2D energyBar1;
public Texture2D energyBar2;
public Texture2D powerSupply;
public Texture2D turretMount;
public Texture2D defenseWall;
public Texture2D antiInfantry;
public Texture2D antiGroundWeak;
public Texture2D antiGroundStrong;
public Texture2D antiAirWeak;
public Texture2D antiAirStrong;
public Texture2D tunnel;
//---------------------------------------TOWER SELECTIONN CONDITIONALS--------------------------
private int powerSupplyOn = 0;
private int turretMountOn = 0;
private int defenseWallOn = 0;
private int antiInfantryOn = 0;
private int antiGroundWeakOn = 0;
private int antiGroundStrongOn = 0;
private int antiAirWeakOn= 0;
private int antiAirStrongOn = 0;
private int tunnelOn = 0;
public GUIStyle textStyle;
private float money;
private float energy;
private float energyMax;
public float energyBar1_length;
public float energyBar2_length;
public Camera mapCam;
public Rect windowRect;
public Rect windowRectEnable;
public Rect windowRectDisabled = (new Rect(5000,5000,Screen.width *0.75f, Screen.height * 0.75f));
private Texture2D pauseCurrent;
private Texture2D structuresCurrent;
private Texture2D upgradeCurrent;
private Texture2D mapCurrent;
private int windowIDSelect;
//-------------------------------------TOGGLES--------------------------------------------------
bool pauseOn;
bool structuresOn;
bool upgradeOn;
bool mapOn;
// ---------------------------------- SET DECLARATION STANDARDS-----------------------------
void Start () {
pauseCurrent = pause;
structuresCurrent = structures;
upgradeCurrent = upgrade;
mapCurrent = map;
pauseOn = false;
structuresOn = false;
upgradeOn = false;
mapOn = false;
windowRect = windowRectDisabled;
}
// Update is called once per frame
void Update ()
{
money = GetComponent <General_Stats>().resources;
energy = gameObject.GetComponent <General_Stats>().energyUsed;
energyMax = gameObject.GetComponent <General_Stats>().energy;
energyBar1_length = (Screen.width * 0.6f *((GetComponent <General_Stats>().energy / (GetComponent <General_Stats>().energyMax))));
energyBar2_length = (Screen.width * 0.6f *((GetComponent <General_Stats>().energyUsed / (GetComponent <General_Stats>().energyMax))));
windowRectEnable = (new Rect(Screen.width * 0.125f, Screen.height * 0.12f ,Screen.width *0.75f, Screen.height * 0.75f));
if (Input.GetMouseButtonUp (0))
{
if (structuresOn == true)
{
structuresCurrent = structures_on;
pauseOn = false;
upgradeOn = false;
mapOn = false;
windowIDSelect = 1;
windowRect = windowRectEnable;
}
else if (structuresOn == false)
{
structuresCurrent = structures;
windowRect = windowRectDisabled;
}
if (pauseOn == true)
{
pauseCurrent = pause_on;
structuresOn = false;
upgradeOn = false;
mapOn = false;
windowIDSelect = 0;
windowRect = windowRectEnable;
}
else if (pauseOn==false)
{
pauseCurrent = pause;
windowRect = windowRectDisabled;
}
if (upgradeOn == true)
{
upgradeCurrent = upgrade_on;
mapOn = false;
pauseOn = false;
structuresOn = false;
windowIDSelect = 2;
windowRect = windowRectEnable;
}
else if (upgradeOn == false)
{
upgradeCurrent = upgrade;
windowRect = windowRectDisabled;
}
if (mapOn == true)
{
mapCurrent = map_on;
pauseOn = false;
upgradeOn = false;
structuresOn = false;
windowIDSelect = 3;
windowRect = windowRectEnable;
}
else if (mapOn == false)
{
mapCurrent = map;
windowRect = windowRectDisabled;
}
}
}
void OnGUI() {
// -----------------------------MONEY-----------------------------------------------
GUI.DrawTexture (new Rect(Screen.width * 0.3f , -1 ,Screen.width *0.4f,Screen.height *0.1f), moneyHolder);
GUI.Label (new Rect(Screen.width * 0.465f, 0 , Screen.width * 0.2f, Screen.height * 0.1f),("$ ")+(money.ToString()), textStyle);
//------------------------------PAUSE--------------------------------------------------
if (GUI.Button (new Rect (0,0, Screen.width * 0.07f, Screen.height *0.16f), pauseCurrent))
{
pauseOn = !pauseOn;
}
//---------------------------UPGRADED-----------------------------------------------------------
if (GUI.Button (new Rect (Screen.width * 0.93f, Screen.height* 0.84f, Screen.width * 0.07f, Screen.height * 0.16f), upgradeCurrent))
{
upgradeOn = !upgradeOn;
}
//-----------------------------STRUCTURES--------------------------------------------------
if (GUI.Button (new Rect (Screen.width * 0, Screen.height* 0.84f, Screen.width * 0.07f, Screen.height * 0.16f), structuresCurrent))
{
structuresOn = !structuresOn;
}
// -----------------------------------MAP-------------------------------------------------------
if (GUI.Button (new Rect (Screen.width * 0.93f, Screen.height * 0, Screen.width * 0.07f, Screen.height * 0.16f), mapCurrent))
{
mapOn = !mapOn;
}
// ------------------------------------ENERGY BAR ----------------------------------------------------
GUI.DrawTexture (new Rect (Screen.width *0.34f, Screen.height * 0.84f, Screen.width * 0.3f, Screen.height * 0.1f), energyELabel);
GUI.DrawTexture (new Rect (Screen.width *0.2f, Screen.height * 0.91f, Screen.width * 0.6f, Screen.height * 0.1f), energyEmpty);
GUI.DrawTexture (new Rect (Screen.width *0.2f, Screen.height * 0.91f, energyBar1_length, Screen.height * 0.1f), energyBar1);
GUI.DrawTexture (new Rect (Screen.width *0.2f, Screen.height * 0.91f, energyBar2_length, Screen.height * 0.1f), energyBar2);
GUI.Label (new Rect (Screen.width *0.475f, Screen.height * 0.94f, Screen.width *0.6f, Screen.height * 0.1f),((energy.ToString()) + (" / ") + (energyMax.ToString())), textStyle);
//--------------------------SELECTION MENUS------------------------------------------------------------
GUI.Window(windowIDSelect, windowRect, DoMyWindow, "");
}
void DoMyWindow(int windowID)
{
//---------------------------------------------------PAUSE---------------------------------------------------------------------
if (windowID ==0)
{
}
//----------------------------------------------------TOWER SELECTION---------------------------------------------------------------
else if (windowID ==1)
{
//-----POWER SUPPLY-----
if (GUI.Button(new Rect (0,0,Screen.width *0.12f, Screen.height *0.2f),powerSupply))
{
powerSupplyOn++;
if (powerSupplyOn == 1)
{
}
else if (powerSupplyOn == 2)
{
gameObject.GetComponent <Tower_Placement>().powerSupply = true;
powerSupplyOn = 0;
structuresOn = false;
}
}
//----TURRET MOUNT ----
if (GUI.Button(new Rect (0,Screen.height *0.27f,Screen.width *0.12f, Screen.height *0.2f),turretMount))
{
turretMountOn++;
if (turretMountOn == 1)
{
}
else if (turretMountOn ==2)
{
gameObject.GetComponent <Tower_Placement>().turretMount = true;
turretMountOn = 0;
structuresOn = false;
}
}
// ----DEFENSE WALL ----
if (GUI.Button(new Rect (0,Screen.height *0.54f,Screen.width *0.12f, Screen.height *0.2f),defenseWall))
{
defenseWallOn++;
if (defenseWallOn == 1)
{
}
else if (defenseWallOn == 2)
{
gameObject.GetComponent <Tower_Placement>().defenseWall = true;
defenseWallOn = 0;
structuresOn = false;
}
}
// ---- ANTI INFANTRY----
if (GUI.Button(new Rect(Screen.width * 0.155f, Screen.height *0.54f, Screen.width *0.12f, Screen.height *0.2f),antiInfantry))
{
antiInfantryOn++;
if (antiInfantryOn ==1)
{
}
else if (antiInfantryOn == 2)
{
gameObject.GetComponent <Tower_Placement>().antiInfantry = true;
antiInfantryOn = 0;
structuresOn = false;
}
}
// ----ANTI GROUND WEAK----
if (GUI.Button(new Rect(Screen.width * 0.310f, Screen.height *0.54f, Screen.width *0.12f, Screen.height *0.2f),antiGroundWeak))
{
antiGroundWeakOn++;
if (antiGroundWeakOn == 1)
{
}
else if (antiGroundWeakOn ==2)
{
gameObject.GetComponent <Tower_Placement>().antiGroundWeak = true;
antiGroundWeakOn = 0;
structuresOn = false;
}
}
// ----ANTI GROUND STRONG----
if (GUI.Button(new Rect(Screen.width * 0.465f, Screen.height *0.54f, Screen.width *0.12f, Screen.height *0.2f),antiGroundStrong))
{
antiGroundStrongOn++;
if (antiGroundStrongOn ==1)
{
}
else if (antiGroundStrongOn ==2)
{
gameObject.GetComponent <Tower_Placement>().antiGroundStrong = true;
antiGroundStrongOn = 0;
structuresOn = false;
}
}
//---- ANTI AIR WEAK----
if (GUI.Button(new Rect(Screen.width * 0.620f, Screen.height *0.54f, Screen.width *0.12f, Screen.height *0.2f),antiAirWeak))
{
antiAirWeakOn++;
if (antiAirWeakOn ==1)
{
}
else if (antiAirWeakOn == 2)
{
gameObject.GetComponent <Tower_Placement>().antiAirWeak = true;
antiAirWeakOn = 0;
structuresOn = false;
}
}
// ---- ANTI AIR STRONG----
if ( GUI.Button(new Rect(Screen.width * 0.620f, Screen.height *0.27f, Screen.width *0.12f, Screen.height *0.2f),antiAirStrong))
{
antiAirStrongOn++;
if (antiAirStrongOn == 1)
{
}
else if (antiAirStrongOn ==2)
{
gameObject.GetComponent <Tower_Placement>().antiAirStrong = true;
antiAirStrongOn = 0;
structuresOn = false;
}
}
// ----TUNNEL ----
GUI.Button(new Rect(Screen.width * 0.620f, 0, Screen.width *0.12f, Screen.height *0.2f),tunnel);
{
tunnelOn++;
if (tunnelOn == 1)
{
}
else if (tunnelOn == 2)
{
gameObject.GetComponent <Tower_Placement>().tunnel = true;
tunnelOn = 0;
structuresOn = false;
}
}
}
//-----------------------------------------UPGRADES----------------------------------------
else if (windowID ==2)
{
}
//---------------------------------------MAP-----------------------------------------------
else if (windowID == 3)
{
}
}
}
that's a lot of code which would need other assets (2 other scripts from a quick scan and some textures to be assigned) in order for somebody to run it themselves...
you could strip it down the something a lot simpler that does work and slowly add back functionality...
i realize that's not the help you were looking for - if i weren't getting on a plane in a few hours, i'd take a look but it's not a 5 $$anonymous$$ute job... and i'd rewrite chunks of it too - cache some components, add some switch statements, etc.
you might get some takers if you created a test project with the $$anonymous$$imal amount of assets and linked to it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with destroying guiRect? 0 Answers
Display GUI texture on specific variable value? 0 Answers
Texture's Alpha is not Transparent when I use it as a GUI.label 1 Answer
GUI Variable Declaration problem 0 Answers