- Home /
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.
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
}
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.
Arf my mistake I forget to change my boolean (I edit my answer)
Hi,thanks @Wonde please use mention code by Veldars,its working fine.
Well thank you @Veldars it is working now, I appreciate this a lot.
I answered you one hour ago... But still waiting for moderation...
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(); }
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.
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;
}
Your answer
Follow this Question
Related Questions
[C#] Both button check with delay. 1 Answer
Multiple Cars not working 1 Answer
Unity 2D Make An Object Disappear On Click 0 Answers
Add Listeners to array of Buttons 2 Answers