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
0
Question by Jojoba007 · Aug 08, 2017 at 06:39 PM · functionsif statementmethods

Check a bool method with if statement

How do I check if a function that is given a bool is true?

In this example below the if statement isn't working. I would like to check from void Start() if PlayMusic() is true. What is the proper way to type this?

 void Start() {
 
         if (PlayMusic() == true) {
                     DoSomething;
     }
 }
 
 public void PlayMusic (bool play) {
 
     
         if (play) {
              
                            DoSomething;
             } 
         } else {
             DoSomething;
         }
 
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by RealAso · Aug 08, 2017 at 10:24 PM

Try this

 public PlayMusic (bool play) {
  
      
          if (play) {
               
                             return true;
              } 
          } else {
              return false;
          }

by returning a true or false, when using the method it will return true or false depending on the method ALSO NOTE

HAVING 'VOID' IN THE METHOD DEFINITION WILL MAKE IT SO IT WILL NOT RETURN ANYTHING. REMOVE THE 'VOID' (like in the code).

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 Jojoba007 · Aug 09, 2017 at 09:06 AM 0
Share

Thank you for your respons.

And is the following code I typed the right way to check the bool? What is the right way to type this, because I get an error on this line:

  if (Play$$anonymous$$usic() == true) {
                      DoSomething;
avatar image RealAso Jojoba007 · Aug 09, 2017 at 03:24 PM 0
Share

What is the error that you are getting? I need to know so I can see what's wrong.

avatar image Jojoba007 RealAso · Aug 09, 2017 at 05:09 PM 0
Share

Got it working, this is my code. Thanks for helping me out.

 // Changing the song when loading a specific level
     void ChangeSong() {
 
         if ($$anonymous$$usicState.Get$$anonymous$$usicState () == 1) {
             if (lastScene == "$$anonymous$$ain$$anonymous$$enu") {
                 audioSource.PlayOneShot ($$anonymous$$ain$$anonymous$$enuSongAU, 0.4f);
 
                 Debug.Log ("Var lastScene is now: " + lastScene);
 
             } else if (lastScene == "GameStartInfo") {
                 audioSource.Stop ();
                 audioSource.PlayOneShot (GreenHillsSongAU, 0.7f);
 
                 Debug.Log ("Var lastScene is now: " + lastScene);
         
             } else if (lastScene == "GamePlay1") {
                 audioSource.Stop ();
                 audioSource.PlayOneShot (GreenHillsSongAU, 0.7f);
 
                 Debug.Log ("Var lastScene is now: " + lastScene);
         
             } else if (lastScene == "GamePlay2") {
                 audioSource.Stop ();
                 audioSource.PlayOneShot (CaveSongAU, 0.7f);
 
                 Debug.Log ("Var lastScene is now: " + lastScene);
         
             } else if (lastScene == "$$anonymous$$ain$$anonymous$$enuLava") {
                 audioSource.Stop ();
                 audioSource.PlayOneShot ($$anonymous$$ain$$anonymous$$enuSongAU, 0.4f);
 
                 Debug.Log ("Var lastScene is now: " + lastScene);
             }
         } else {
 
             audioSource.Stop ();
         }
 
     }
 
 
     public void Play$$anonymous$$usic (bool play) {
 
     
         if (play) {
             if (!audioSource.isPlaying) {
 
                     ChangeSong();
             }
         
         } else {
             if (audioSource.isPlaying) {
             }
             audioSource.Stop ();
         
         }
 
 
     }
 }

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

111 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 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 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 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 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 avatar image

Related Questions

How can I change a function from another script? 1 Answer

How do I call a method with variables in it? 1 Answer

Method Group 1 Answer

List.FindIndex 1 Answer

How can i pass a method like a parameter Unity3d C#? 3 Answers


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