- Home /
Weird Boolean Problem c#
i have this weird problem: i have a button script with 4 booleans, and the OnMouseDown Script doesnt work right, when i press the button, it changes the boolean and sets the texture, works fine, but when i click it again, it should play a different sound, and change the texture back, instead, it plays the exact same sound and doesnt do anything else.
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorButton : MonoBehaviour {
public Light ButtonOpenLight;
public Light ButtonClosedLight;
public Light ButtonLockedLight;
public Renderer Material;
public AudioSource LockedClickSound;
public Material Open;
public Material Closed;
public Material Locked;
public Material Defect;
public bool DoorOpen;
public bool DoorClosed;
public bool DoorLocked;
public bool DoorDefect;
public GameObject Crosshair;
public GameObject DoorOrObjectToCall;
private void Start()
{
ChangeGraphic();
}
public void OnMouseEnter()
{
Crosshair.GetComponent<HoverVisualization>().MEnter();
}
public void OnMouseExit()
{
Crosshair.GetComponent<HoverVisualization>().MExit();
}
public void ChangeGraphic()
{
if (DoorOpen == true)
{
ButtonOpenLight.enabled = true;
ButtonLockedLight.enabled = false;
ButtonClosedLight.enabled = false;
Material.material = Open;
}
if (DoorLocked == true)
{
ButtonOpenLight.enabled = false;
ButtonLockedLight.enabled = true;
ButtonClosedLight.enabled = false;
Material.material = Locked;
}
if (DoorClosed == true)
{
ButtonOpenLight.enabled = false;
ButtonLockedLight.enabled = false;
ButtonClosedLight.enabled = true;
Material.material = Closed;
}
if (DoorDefect == true)
{
ButtonOpenLight.enabled = false;
ButtonLockedLight.enabled = false;
ButtonClosedLight.enabled = false;
Material.material = Defect;
}
}
void OnMouseDown ()
{
if (DoorOpen == true)
{
DoorOrObjectToCall.GetComponent<DoorScript>().OpenedDoor();
DoorOpen = false;
DoorClosed = true;
ChangeGraphic();
}
if (DoorLocked == true)
{
LockedClickSound.Play();
}
if (DoorClosed == true)
{
DoorOrObjectToCall.GetComponent<DoorScript>().ClosedDoor();
DoorOpen = true;
DoorClosed = false;
ChangeGraphic();
}
if (DoorDefect == true)
{
Debug.Log("Player clicked a useless button...");
//DefectScriptObject.GetComponent<DoorScript>().DefectDoor(); - Probably Gonna Be Removed ;)
}
}
public void ClosedOnly()
{
ButtonOpenLight.enabled = false;
ButtonLockedLight.enabled = false;
ButtonClosedLight.enabled = true;
ChangeGraphic();
}
}
i can also record a small video of the problem, but i dont think its needed, if it is, just ask :)
you need to change those if
statements to else if
- otherwise you're setting them immediately before the next check...
Then i still had the same problem with my actual door, but its fixed now :)
Answer by petraszd · Jan 05, 2017 at 06:58 AM
The problem is in OnMouseDown
method. When DoorOpen
is true
you are executing both code blocks:
if (DoorOpen == true) // If this is true
{
DoorOrObjectToCall.GetComponent<DoorScript>().OpenedDoor();
DoorOpen = false;
DoorClosed = true; // Than you are setting DoorClosed to true
ChangeGraphic();
}
// ...
// Remember your method is not terminated so it runs further commands
if (DoorClosed == true) // At this point DoorClosed is set to true
{
DoorOrObjectToCall.GetComponent<DoorScript>().ClosedDoor();
DoorOpen = true; // So, you are reseting DoorOpen back to true again
DoorClosed = false;
ChangeGraphic();
}
So, correct way to do it would be:
if (DoorOpen == true)
{
// ...
}
else if (DoorClosed == true)
{
// ...
}
Side note: I would not recommend to have four properties DoorOpen
, DoorClosed
, DoorLocked
and DoorDefect
. Especially if a door can have only one state at the given moment. If a door can be either opened or closed or locked or defected, than I would have one property DoorState enum DoorStates {Opened, Closed, Locked, Defected}; // ... DoorStates DoorState;
That fixed the problem! But i had to rewrite the script because it didnt work with a second button on the other side, so now the button has 1 hitbox that just looks like 2 buttons. I also have 4 prefabs ins$$anonymous$$d of the 4 booleans, after that i still had a weird problem, came back to this post, and it works now!