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
0
Question by Nicolas64pa · Jul 23, 2021 at 04:49 PM · bool

Why doesnt this bool take effect even though it does change?

Hello, i have been writing some code to make an attack with a cooldown and specific duration, this is the code

    if (Input.GetKeyDown(KeyCode.E) && ableToAttack)
         {
             ableToAttack = false;
             StartCoroutine(attackDuration(2));
             StartCoroutine(attackCD(5));
             toggleAttack = !toggleAttack;
             attack.SetActive(toggleAttack);
             attackParticles.SetActive(toggleAttack);                       
             burning = toggleAttack;
             burningSound.Play();            
             if (toggleAttack == false)
             {
                 burningSound.Stop();
             }
         }  

The code works as intended, the bool toggleAttack changes from false(its set value) to true when pressing E and it also turns to false when the 2 seconds the attackDuration corountine has get to 0, but the gameobjects attack and attackParticles dont change to false, they remain in the scene when they should have been turned off, any idea as to why this is happening? Any help would be appreciated

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 tyruji · Jul 23, 2021 at 05:53 PM 0
Share

Can you post the coroutine methods?

avatar image Nicolas64pa tyruji · Jul 23, 2021 at 05:58 PM 0
Share
 private IEnumerator attackCD(float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
         ableToAttack = true;
     }
     private IEnumerator attackDuration(float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
         toggleAttack = false;
     }

1 Reply

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

Answer by tyruji · Jul 23, 2021 at 06:06 PM

You are not calling SetActive(false) on your gameobjects after these 2 seconds pass, so I suggest you call it in your "attackDuration" method, just after setting "toggleAttack".

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 Nicolas64pa · Jul 23, 2021 at 06:29 PM 0
Share

but SetActive is set to toggleAttack, which would be false after the 2 seconds pass, wouldnt that have to work? Also how would i go about calling it in "attackDuration"?

avatar image tyruji · Jul 23, 2021 at 07:32 PM 0
Share

No, it won't work - you are just setting the bool value of "toggleAttack", you are not updating the active state of any game object, here is how your coroutine should look like:

      private IEnumerator attackDuration( float waitTime )
      {
          yield return new WaitForSeconds( waitTime );
          toggleAttack = false;
          attack.SetActive( false );
          attackParticles.SetActive( false );
      }
avatar image Nicolas64pa tyruji · Jul 23, 2021 at 07:54 PM 0
Share

just tried it and it works perfectly, thank you very much

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

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

Related Questions

Slerpy Spider Won't Behave (Chaining Slerps) 1 Answer

Quick question about arrays and MonoDevelop 1 Answer

NullReferenceException: When accessing bools from another game object's script. 1 Answer

Problem with creating static bool... 1 Answer

Set a Bool Through Function 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