Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 wonde · Jul 21, 2015 at 08:55 AM · c#unity 5buttonboolvoid

How to make on/off-like gui-button?

Hello everyone, basically I need a gui-button that turns lights ON when pressed first time and OFF when pressed second time and so on, like a lightswitch.

Here is the code that I've managed to create so far:

 using UnityEngine;
 using System.Collections;
 
 public class LightCtrl : MonoBehaviour {
 
     //This Script controls cars lights
     public Light Lowbl; //Assign a Lowbeam-left light
     public Light Lowbr; // Assign a Lowbeam-right light
 
     private float diff = 1;
     
     void FixedUpdate(){
         diff = (Screen.width / 16.8f) / 100;
     }
     void OnGUI() { 
         if (GUI.Button (new Rect (1110 * diff, 640 * diff, 60 * diff, 60 * diff), "LOW")) {
             BtnLow();
         }
     }
     private bool isClicked = false;
 
     void BtnLow(){
         if(isClicked){
             isClicked = LowOn();
         }
         else{
             isClicked = LowOff();
         }
     }
     void LowOn(){
         Lowbl.intensity = 3.0f;
         Lowbr.intensity = 3.0f;
     }
     void LowOff(){
         Lowbl.intensity = 0.0f;
         Lowbr.intensity = 0.0f;
     }
 }

And I get error "Cannot implicitly convert type void' to bool'" and per usual I'm not very fond with coding so I appreciate if you can answer with example code/script with explaining. I hope that you understand me and thanks for everyone who helps in advance.

Comment
Add comment
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

3 Replies

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

Answer by Veldars · Jul 21, 2015 at 09:00 AM

I, you just forget to return somthing in your functions ^^

      void BtnLow(){
          if(!isClicked){ // Here test if light are NOT (the "!") been clicked
              isClicked = LowOn(); // Here you ask for a boolean return
          }
          else{
              isClicked = LowOff();
          }
      }
      bool LowOn(){ // so here you must say that this function return a boolean (void mean that the function return nothing)
          Lowbl.intensity = 3.0f;
          Lowbr.intensity = 3.0f;
          return true; // return true when it's on
      }
      bool LowOff(){
          Lowbl.intensity = 0.0f;
          Lowbr.intensity = 0.0f;
          return false; // return false when it's off
      }
Comment
Add comment · Show 5 · 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 wonde · Jul 21, 2015 at 09:31 AM 0
Share

Hi, thank you @Veldars for quick response. I changed both void to bool and put return, but somehow it just doesn't work I only get either ON function or OFF function but not both.

avatar image Veldars · Jul 21, 2015 at 09:41 AM 1
Share

Arf my mistake I forget to change my boolean (I edit my answer)

avatar image ramp · Jul 21, 2015 at 09:50 AM 0
Share

Hi,thanks @Wonde please use mention code by Veldars,its working fine.

avatar image wonde · Jul 21, 2015 at 09:53 AM 0
Share

Well thank you @Veldars it is working now, I appreciate this a lot.

avatar image ReCoF · Jul 21, 2015 at 09:54 AM 0
Share

I answered you one hour ago... But still waiting for moderation...

avatar image
2

Answer by ramp · Jul 21, 2015 at 09:15 AM

Hello,

void type can not convert in bool,it can be use only for condition true or false,

your modified code is given below.

if(isClicked) { LowOn(); }

else { LowOff(); }

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 wonde · Jul 21, 2015 at 09:44 AM 0
Share

Hi @ramp, I also tried this and it gave me same thing as below, it only gives me ON function or OFF function depending if " private bool isClicked = " is true or false.

avatar image
1

Answer by ReCoF · Jul 21, 2015 at 10:57 AM

Hi,

What you need is a Toggle, which is a Button that has two state.

Actually, i'm doing everything from script so this is what it looks like for your example :

 public Toggle toggle; /* Assign from the editor */

 void Start() {
    toggle.onValueChanged.AddListener(ChangeLow);
 }

 public void ChangeLow(bool state) 
 {
     if(state)
     {
         LowOn();
     }else{
         LowOff();
     } 
 }

 void LowOn(){
     Lowbl.intensity = 3.0f;
     Lowbr.intensity = 3.0f;
 }
 void LowOff(){
     Lowbl.intensity = 0.0f;
     Lowbr.intensity = 0.0f;
 }


Comment
Add comment · 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

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

25 People are following this question.

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

Related Questions

[C#] Both button check with delay. 1 Answer

Why there is an 'SerializationException: serializationStream supports seeking, but its length is 0' error when I click the load button? 2 Answers

Multiple Cars not working 1 Answer

Unity 2D Make An Object Disappear On Click 0 Answers

Add Listeners to array of Buttons 2 Answers


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