- Home /
Accessing a public Bool from a Ui Button?
Hey, i got a script which looks like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#pragma warning disable 0219
#pragma warning disable 0162
#pragma warning disable 0414
#endif
public class boolz : MonoBehaviour {
#if UNITY_EDITOR
// states
public bool editorIsLoading;
public bool erase;
private int isRotated;
private int isRotatedH;
private bool is2D;
private bool isOrtho;
public bool canBuild;
private bool isCurrentStatic;
private bool isCamGridMoving;
private bool isBuildingTC;
private bool isContinuesBuild;
private bool isSortingByNameEnabled;
public bool isBuildingMass;
private bool isMouseDown;
private bool isInTopView;
//and so on...
so what i am trying to do is for example activating the "public bool erase;" for example via an UI button. so using the unity event system (that on click() thing from the Button Component) but i can not find the bool in the drop down menu. next question after that would be how can i prevent that two bools are activated, for example "public bool erase;" and "public bool canBuild;" should'nt be active at the same time
Answer by fafase · Nov 06, 2015 at 12:31 PM
You cannot use instance variable directly, you can pass a static value like either true or false if your method looks like this:
public void MyMethod(bool value);
But that is not what you want.
You need to pass the script as parameter:
public void MyMethod(boolz script){
script. editorIsLoading = ! script. editorIsLoading;
}
this would switch the value on/off.
So add a script with that method to an object, drag the object to the button listener slot. Then look for the method. You now have a boolz slot showing up. Drag the bool component you want to use in that method.
ah, okay. got it. but how do i actually put a reference to the other sript (boolz) to my new script which includes this:
public void $$anonymous$$y$$anonymous$$ethod(boolz script){
script. editorIsLoading = ! script. editorIsLoading;
}
do i need to put something like "public script boolz" on top of the script?
What do you need the reference for? You have the reference to be used within the method.
If you need to use that value later, add a bool and :
private bool value = false;
public void $$anonymous$$y$$anonymous$$ethod(boolz script){
script. editorIsLoading = ! script. editorIsLoading;
this.value = script. editorIsLoading;
}
if you need to keep a reference to boolz component, then you don't need to pass it as parameter, just add a reference and drag it.
public boolz myBoolz;
public void $$anonymous$$y$$anonymous$$ethod(){
myBoolz. editorIsLoading = ! myBoolz. editorIsLoading;
}
ahhh, oh okay, so its better to put this line:
public void $$anonymous$$y$$anonymous$$ethod(boolz script){
script. editorIsLoading = ! script. editorIsLoading;
}
inside the script where the bools (from the first post) are, right? I somehow thought it needs to be a new script, sorry