Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Firechicken · Jan 04, 2017 at 08:43 AM · c#buttonbooleanonmousedown

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 :)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jan 03, 2017 at 03:02 PM 2
Share

you need to change those if statements to else if - otherwise you're setting them immediately before the next check...

avatar image Firechicken · Jan 05, 2017 at 09:16 AM 0
Share

Then i still had the same problem with my actual door, but its fixed now :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Firechicken · Jan 05, 2017 at 09:15 AM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If statement breaking button color copy 0 Answers

Unable to show gameobjects at specific timing 0 Answers

Multiple Cars not working 1 Answer

Can anyone help me in Bolt 0 Answers

my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges