Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by Grievemourne · Jun 04, 2015 at 02:53 PM · booleancheck

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;
     }
 }
 
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 Bunny83 · Jun 04, 2015 at 03:02 PM 0
Share

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.

avatar image Grievemourne · Jun 04, 2015 at 06:28 PM 0
Share

Have fun.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 3 · 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 Grievemourne · Jun 05, 2015 at 07:47 AM 0
Share

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.

avatar image Eno-Khaon · Jun 05, 2015 at 11:18 PM 1
Share

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.

avatar image Grievemourne · Jun 06, 2015 at 11:33 AM 0
Share

I've only ever used enabled for that purpose. Didn't know about this legacy. Thanks for the explanation.

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

22 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

Related Questions

Boolean Uncheck Inspector if False 2 Answers

Check slected mission status in inspector 1 Answer

How do I check a boolean from another script in C#? 0 Answers

Does anyone know about BeingChangeCheck function? 0 Answers

Reference boolean check from other script (not working) 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