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 Salmjak · Feb 15, 2016 at 01:57 PM · c#coroutinebooleanchange

Local bool won't change inside coroutine

EDIT: I removed networking from title since I was able to reproduce this in a regular monobehaviour. So it's all down to regular Coroutines not being able to change a variable.

The bool "skill1OnCD" won't change when passed to a coroutine.

 private bool skill1OnCD;
 
 void Start(){
 skill1OnCD = false;
 }
 
     void Update()
     {
         if (isLocalPlayer)
         {
             if (Input.GetKeyDown(skillKey1))
             {
                 Debug.LogError(skill1OnCD);
                 if (!skill1OnCD)
                 {
                     string skillName = skillDatabase.skills[0].displayName;
                     ActivateSkill(skillName);
                     skill1OnCD = true;
                     StartCoroutine(ReduceCooldown(skillDatabase.skills[0].cooldown, skill1cdImage, skill1OnCD));
                 }
                 else
                 {
                     Debug.LogError("Skill is on CD");
 
                 }
             }
 }
 
 IEnumerator ReduceCooldown(float duration, Image skillCD, bool onCD)
     {
         if (!isLocalPlayer)
         {
             yield break;
         }
         Debug.LogError("Started cooldown for " + duration + " seconds");
         skillCD.fillAmount = 1f;
         float stopTime = Time.realtimeSinceStartup + duration;
         while(Time.realtimeSinceStartup <= stopTime)
         {
             float lerpValue = Mathf.Lerp(skillCD.fillAmount, (stopTime - Time.realtimeSinceStartup) / stopTime, Time.deltaTime * 10f);
             skillCD.fillAmount = lerpValue;
             yield return new WaitForEndOfFrame();
         }
         skillCD.fillAmount = 0f;
         Debug.LogError("Cooldown finished, setting CD to false");
         onCD = false;
         Debug.LogError(onCD);
     }

So, the Debug error in the coroutine says that the variable onCD has been set to false. When I try to press the key again the debug error says the variable is true. The script inherit NetworkBehaviour, is this some weird sync error? The variable isn't set to Synv so I dont get why it would.

alt text

2016-02-15-14-51-00-unity-personal-64bit-hnsunity.png (17.9 kB)
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
0
Best Answer

Answer by Salmjak · Feb 15, 2016 at 04:18 PM

Once I removed the NetworkBehaviour from my Google searches it was quite easy to find an answer. Bool is a Value type and does not pass a reference. Credit to @Baste and @NoseKills

A solution to the problem can be found here.

Comment
Add comment · Show 4 · 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 Baste · Feb 15, 2016 at 04:30 PM 1
Share

Wrong; this has nothing to do with ReduceCooldown being a coroutine, and all to do with skill1OnCD being a bool. Bool variables are always passed by value. You can try to do this:

 void Awake() {
     SetToTrue(skill1OnCD);
     print(skill1OnCD);
 }

 void SetToTrue(bool b) {
     b = true;
 }

The print-call will print "false".

avatar image Salmjak Baste · Feb 15, 2016 at 07:02 PM 0
Share

@Baste so this is the case for all value types? Have I really just been lucky (or unlucky depending on how you see it) that I've never ran into this problem before?

So this should be the same for:

 int num = 0;
 
 void Awake(){
 Increment(num);
 print(num);
 }
 
 void Increment(int i){
 i++;
 }

avatar image Baste Salmjak · Feb 16, 2016 at 10:14 AM 1
Share

Yes, that will still print 0.

You could pass by reference;

 void Awake() {
     Increment(ref num);
     print(num); //prints 1
 }

 void Increment(ref int i) {
     i++;
 }

But that's not possible in an IEnumerator (it won't compile)

avatar image NoseKills · Feb 15, 2016 at 05:07 PM 1
Share

What @Baste said. @Salmjak read up on value & reference types to avoid future headaches.

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

112 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

Related Questions

StartCoroutine not listening to parameters 1 Answer

WaitForSeconds doesn't wait if time is too low 0 Answers

Why doesn't the collider work? 1 Answer

Weird problem with simple boolean and "if" statement [C#] 0 Answers

animator boolean 2 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