Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by AedanCGraves · May 18, 2017 at 07:28 AM · c#booleanboolbooleans

Call a function When a bool changes value?

I'm trying to call an animation/sound when a bool becomes true. How can I do that when it changes from false to true and not simply every frame that it is true?

Comment
Add comment · Show 1
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 SohailBukhari · May 18, 2017 at 07:41 AM 0
Share

$$anonymous$$ake unity event and when your bools turn to true then invoke your event or what you want.

2 Replies

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

Answer by Hellium · May 18, 2017 at 07:29 AM

Use properties : https://msdn.microsoft.com/en-us/library/aa288470(v=vs.71).aspx

 private bool myBool ;
 
 public bool MyBool
 {
    get { return myBool ; }
    set
    {
        if( value == myBool )
            return ;
 
        myBool = value ;
        if( myBool )
        {
            // Play sound
        }    
    }    
 }

Comment
Add comment · Show 13 · 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 AedanCGraves · May 18, 2017 at 05:09 PM 0
Share

Ok. I kind of get it... which bool do I use in the rest of the code? I see we make a bool function and a regular bool, which one should I use to set true/false?

avatar image Hellium AedanCGraves · May 18, 2017 at 06:16 PM 0
Share

You have to use the property (getter and setter) i.e : $$anonymous$$yBool

avatar image AedanCGraves Hellium · May 18, 2017 at 06:29 PM 0
Share

It worked! thanks!

avatar image leonzu · Dec 18, 2017 at 03:53 PM 0
Share

Hello,

How could I reuse this code for a high number of booleans, like 100, or maybe an array of booleans?

Thanks in advance!

avatar image Hellium leonzu · Dec 18, 2017 at 04:00 PM 1
Share
 private bool arrayOfBool = new bool[100];
 
 public void SetBoolValue( bool value, int index )
 {
     if( index < 0 || index >= arrayOfBool )
         throw new System.ArgumentOutOfRangeException("Index value must be between 0 and " + arrayOfBool.Length );
 
     if( arrayOfBool[index] == value )
         return ;
 
     arrayOfBool[index] = value ;
 
     if( arrayOfBool[index] )
        Foo() ;
     else
         Bar() ;
 
 }


avatar image leonzu Hellium · Dec 18, 2017 at 04:18 PM 0
Share

Thanks for the quick answer.

Would I have to call the method periodically with a timer in order to check the value of the booleans?

Show more comments
avatar image Darkgaze · Oct 03, 2018 at 03:10 PM 0
Share

Note, though, that these properties CAN NOT LONGER be shown in the properties editor.

avatar image Hellium Darkgaze · Oct 03, 2018 at 05:19 PM 0
Share

The Unity inspector never shown properties unless you create the appropriate custom inspector because of the potential side effects (like the one in my answer).

However, you can make the "inner variable" visible in the inspector by adding a tag above it in your code:

 [SerializeFiled]
 private bool myBool ;

Obviously, changing the value in the inspector won't call the setter.

avatar image nia0724 · May 19, 2020 at 11:03 PM 0
Share

Okay I know this reply is 3 years ago already. but I think this code just checks when you change the $$anonymous$$yBool variable and not the myBool variable. It's kinda redundant compared to this, isn't it?

 private bool myBool;
 
     public void $$anonymous$$yBool()
     {
         if (myBool)
         {
             myBool = false;
             
         }
         else
         {
             myBool = true;

             // Play Sound
         }
     }
     

would do the same? If not what would be the difference, optimization wise?

$$anonymous$$y main question is ,how can I check the myBool variable and not call $$anonymous$$yBool? because both of our codes is based on calling $$anonymous$$ybool as an event.

avatar image
-2

Answer by toddisarockstar · May 19, 2017 at 01:54 AM

here is a simple answer with an "if" statement if you don't want to get into custom classes.

     bool mybool;
     bool checkit;
     
     void Update(){
     if(mybool!=checkit){
     checkit=mybool;
 
     print("my bool has changed to: "+ mybool);
    if(mybool==true){
 //do stuff here
 }
     }
     }
 
 
 

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 mjonasz · Oct 10, 2018 at 04:45 PM 0
Share

Excellent! I was looking for exactly this myself. Thanks!

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

14 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

Related Questions

Static bool 1 Answer

Boolean somehow becomes false. And an Integer zero. 0 Answers

Referenced Bool is not updating 1 Answer

How do I check if bool is true from another script? 2 Answers

how to boolean global and trigger a command when two booleans are true C# 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