- Home /
Why would a boolean change when checked by another script?
Using debug.log to check the boolean every update, in the script holding the boolean it finds it as false - in another script checking the same boolean, it finds it as true. This is thoroughly confusing to me as the code that would change the boolean to true at any point never activates.
using UnityEngine;
using System.Collections;
public class Connector : MonoBehaviour {
int activeCount;
bool active = false;
public GameObject[] activators;
void Update () {
Debug.Log("Active count: " + activeCount + " - activator length: " + activators.Length + " - active is set to: " + active);
activeCount = 0;
foreach(GameObject activator in activators){
if(activator.GetComponent<Trigger>() != null){
if (activator.GetComponent<Trigger>().active == true){
activeCount += 1;
}
}
}
if(activeCount == activators.Length){
active = true;
}else{
active = false;
}
}
}
using UnityEngine;
using System.Collections;
public class Open : MonoBehaviour {
public float openLimit = 3f;
public bool open = false;
public float openThreshold;
public float openingSpeed = 5.0f;
public int openGridmultiplier = 1;
public bool usesGravity = true;
public bool selfCloses = true;
public float closeThreshold;
public Transform myTransform;
public Vector3 myPos;
public GameObject activator;
Trigger trigger;
Button button;
Connector connector;
void Start(){
closeThreshold = transform.position.y;
openThreshold = transform.position.y + openLimit * openGridmultiplier;
myTransform = transform;
myPos = transform.position;
if(activator.GetComponent<Button>() != null)
{
Debug.Log ("Button linked");
button = activator.GetComponent<Button>();
}
if(activator.GetComponent<Trigger>() != null)
{
trigger = activator.GetComponent<Trigger>();
}
if(activator.GetComponent<Connector>() != null)
{
connector = activator.GetComponent<Connector>();
}
}
public void OpenPortcullis(){
Debug.Log ("Opening PortCullis!");
rigidbody.useGravity = false;
open = true;
rigidbody.velocity = Vector3.zero;
if(transform.position.y < openThreshold){
transform.position = new Vector3(myPos.x, myPos.y + (Time.deltaTime * openingSpeed), myPos.z);
}
else{
transform.position = new Vector3(myPos.x, openThreshold, myPos.z);
}
}
public void ClosePortcullis(){
if(usesGravity != true){
if(selfCloses == true){
if(transform.position.y > closeThreshold){
transform.position = new Vector3(myPos.x, myPos.y - (Time.deltaTime * openingSpeed), myPos.z);
}
else{
//transform.position = new Vector3(myPos.x, closeThreshold, myPos.z);
}
}
}
else
{
rigidbody.useGravity = true;
}
open = false;
}
void Update() {
if(activator.GetComponent<Trigger>() != null){
if(trigger.active == true){
OpenPortcullis();
}else{
ClosePortcullis();
}
}else if(activator.GetComponent<Button>() != null){
if(button.active == true){
OpenPortcullis();
}else{
ClosePortcullis();
}
}else if(activator.GetComponent<Connector>() != null){
Debug.Log(connector.active);
if(connector.active == true){
OpenPortcullis();
}else{
ClosePortcullis();
}
}
myPos = transform.position;
}
}
Post the script that holds the boolean variable and the piece of code that you use in the other script to access that boolean value.
What kind of answers do you expect with a question like this?
Feel free to edit your question to include more information.
Answer by Eno-Khaon · Jun 04, 2015 at 06:50 PM
I could be mistaken, but I'm guessing that it could be an issue with sharing the term "active" with whether the script itself is "active" at the time. Does it still work if you change the name of your boolean variable from "active" to something else?
Wow - well, first time first time I've seen this. There seems to be some kind of automation if the script doesn't find what it wants. Essentially the problem was I forgot to make the active boolean public, but the compiler didn't warn me that it wasn't available, ins$$anonymous$$d, it either found or invented a boolean by the same name and made it true. (which is extra weird when all of the conceivable values of the same name where also false).
Thanks for pointing out that I should try changing the name of the value, though, since that got the script to point out that the value wasn't public.
Well, nothing was made up, really. $$anonymous$$ost Objects in Unity (a GameObject, a script, a RigidBody, a Collider, etc.) have an "active" boolean. This was later replaced by "enabled" for use, but backwards compatibility and general support means that "active" is still technically listed for use.
Because you defined the variable locally into the script, your use of the term in regard to the script was on the basis of using your variable named "active". However, because the variable was, indeed, a private variable, your attempts to read it were running into the Object variable attached to any script, also named "active".
The name was correctly spelled, but was read from the wrong context, and that's why it was able to function incorrectly and not list any errors. Your script was "active" and running, therefore the result of that check was always true.
I've only ever used enabled for that purpose. Didn't know about this legacy. Thanks for the explanation.