- Home /
Toggle Only Works Once
Here is a weird one. I have my texture2d objects placed on the screen. It's supposed to change to another image when clicked. It worked before, but when I added another statement to the mix, it only changed once. After removing the addition, it still only does it once. Here is the code. If there is a better way of doing it, I'm glad to hear.
public bool CombatMode;
public bool RangedMode; public Texture2D PeaceButton; public Texture2D CombatButton;
void OnMouseDown() { if(PeaceButton) Mode (); if(CombatMode=true) {
if(RangedButton) RangedAttackMode();
if(AttackHackButton) HackAttack();
if(AttackSlashButton) SlashAttack();
if(AttackThrustButton) ThrustAttack();
if(ButtonUppercutOn) UppercutAttack(); } (What I want to happen is to enable the attack types if you are in combat mode. Can't figure out how to do that.)
if(CharacterButton) Mirror ();
if(CampButton) Camp();
if(JournalButton) Journal();
if(InventoryButton) Inventory(); }
void Mode()
{ if(!CombatMode) {
guiTexture.texture=CombatButton; CombatMode=true;
Debug.Log("Combat Mode" +CombatMode); } else {
guiTexture.texture=PeaceButton; CombatMode=false; Debug.Log("Peace Mode" +CombatMode); } }
Thanks!
if(Combat$$anonymous$$ode=true) should actually SET Combat$$anonymous$$ode = true and then always returns true (by definition)
On a related note, the statement toggle = !toggle; is sometimes useful.
Also please format your code by selecting it and pushing the 101010 button at the top.
Thank you.
Answer by Gilead7 · Aug 16, 2012 at 08:40 PM
For those that have been following this, I finally have a working solution, which I will share.
private bool isCombatMode=false;
private bool isRangedMode=false;
public Texture2D PeaceButton;
public Texture2D CombatButton;
public Texture2D ChopLabel;
public Texture2D ChopButton;
public Texture2D SlashLabel;
public Texture2D SlashButton;
public Texture2D ThrustLabel;
public Texture2D ThrustButton;
public Texture2D UppercutLabel;
public Texture2D UppercutButton;
public Texture2D RangedLabel;
public Texture2D RangedButton;
public Texture2D RangedButtonOn;
//***********************************
// Rects for Combat Menu //
//***********************************
private Rect chopLabelRect = new Rect(Screen.width/2-242, Screen.height-99, 90, 90);
private Rect slashLabelRect = new Rect(307, Screen.height-99, 90, 90);
private Rect thrustLabelRect = new Rect(217, Screen.height-99, 90, 90);
private Rect uppercutLabelRect = new Rect(127, Screen.height-99, 90, 90);
private Rect rangedLabelRect = new Rect(Screen.width/2-152, Screen.height-99, 90, 90);
private Rect peaceButtonRect = new Rect(Screen.width/2-60, Screen.height-100, 90, 90);
private Rect chopButtonRect= new Rect(Screen.width/2-242, Screen.height-99, 90, 90);
private Rect slashButtonRect= new Rect(307, Screen.height-99, 90, 90);
private Rect thrustButtonRect= new Rect(217, Screen.height-99, 90, 90);
private Rect uppercutButtonRect= new Rect(127, Screen.height-99, 90, 90);
private Rect rangedButtonRect= new Rect(Screen.width/2-152, Screen.height-99, 90, 90);
void OnGUI()
{
if ( GUI.Button(peaceButtonRect, PeaceButton) )
{
CombatMode();
}
if ( isCombatMode)
{
GUI.Button(peaceButtonRect, CombatButton);
if(GUI.Button(chopButtonRect, ChopButton))
HackAttack();
if(GUI.Button(slashButtonRect, SlashButton))
SlashAttack();
if(GUI.Button(thrustButtonRect, ThrustButton))
ThrustAttack();
if(GUI.Button(uppercutButtonRect, UppercutButton))
UppercutAttack();
if(GUI.Button(rangedButtonRect,RangedButton))
RangedMode();
}
else
{
GUI.Label(chopLabelRect, ChopLabel);
GUI.Label(slashLabelRect, SlashLabel);
GUI.Label(thrustLabelRect, ThrustLabel);
GUI.Label(uppercutLabelRect, UppercutLabel);
GUI.Label(rangedLabelRect, RangedLabel);
}
if(isRangedMode)
{
GUI.Button(rangedButtonRect,RangedButtonOn);
}
}
void CombatMode()
{
isCombatMode = ! isCombatMode;
Debug.Log("Combat Mode is:" +isCombatMode);
}
void RangedMode()
{
isRangedMode=!isRangedMode;
Debug.Log("Ranged Mode is:"+isRangedMode);
}
void HackAttack()
{
Debug.Log("Selected Hack Attack!");
}
void SlashAttack()
{
Debug.Log("Selected Slash Attack!");
}
void ThrustAttack()
{
Debug.Log("Selected Thrust Attack!");
}
void UppercutAttack()
{
Debug.Log("Selected Uppercut Attack!");
}
void RangedAttack()
{
Debug.Log("Selected Ranged Attack!");
}
}
Hope that helps someone!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Enabling and Disabling GUI Textures 1 Answer
Distribute terrain in zones 3 Answers
How to make OnGUI Texture able to be clicked 1 Answer
Assigning script-generated textures 0 Answers