Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 GargoylExtreme · Aug 03, 2015 at 04:06 AM · updatefixedupdatebool

Bool not being re-triggered in FixedUpdate or Update

Hi, I've been pounding at this code all day, can't figure it out. I have two turrets that once fired, activate a cooldown state of about 44 seconds, along with shrinking a status bar using time.time (using time.deltatime only shrinks it by 1.0 then stops). I can get the status bar to shrink, and reset the bool, but for some reason the bool won't reset after firing again.

Here is the code for one of the turrets (the one for the second is identical, only 1 replaced with 2 for the second one)

 if (activeCD1)
 {
     Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.red;
     Turret1Status.sizeDelta = new Vector2(tur1width - 1.0f * Time.time, 6);

     if (Turret1Status.sizeDelta.x <= 0)
     {
         activeCD1 = false;
         Turret2Status.sizeDelta = new Vector3(44, 6);
     }
 }
 else
 {
     Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.green;
     Turret1Status.sizeDelta = new Vector3(44, 6);
 }

There is some bizarre de-synch with tur1width and Turret1Status.sizeDelta.x. It properly goes down, and the activeCD1 gets set to false and status bar is reset to normal size. But when the function gets called again, turret1status.sizedelta.x is still 0. I tried everything to set it back to 44, but nothing works. Is there something I'm missing, or am I doing this wrong?

Here is the function that calls activeCD1 to be true incase there's something I missed:

 public void Turret1Off()
 {
      activeCD1 = true;
      Turret1Active = false;
      hp1.SendMessage("TurnOff");
      emit = false;
      beamtake = 0;
      runcycle = 0;
 }
Comment
Add comment · Show 6
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 NoseKills · Aug 03, 2015 at 07:13 AM 1
Share

At least you have this line in there

  Turret2Status.sizeDelta = new Vector3(44, 6);

When all the other lines talk about Turret1.

Looks like you are using a lot of static variables and different scripts for turrets that should probably work exactly the same. Thats a bit hacky way of going about things and that's exactly what makes the code prone to this kind of copy/paste mistakes (if this is the problem. $$anonymous$$aybe I don't understand the whole picture after seeing these bits of code)

avatar image maccabbe · Aug 03, 2015 at 07:25 AM 0
Share

If I understand correctly Turret1Status.sizeDelta.x reaches 0 so you switch a bool. When you switch the bool back you expect Turret1Status.sizeDelta.x to be greater than 0. However none of the shown code actually changes the value of tur1width and Time.time gets bigger over time so why would (tur1width - 1.0f * Time.time) go back over 0?

avatar image Nerevar · Aug 03, 2015 at 07:38 AM 1
Share

also depending on your canvas parameters and your layouts you may not be able to override the size of your UI element. $$anonymous$$ake sure you can manually change it through the inspector at the right time, if not then it is a layout problem.

avatar image NoseKills · Aug 03, 2015 at 11:28 AM 0
Share

@Nerevar has a good point also in the sense that it's a bit dangerous to use a property of an UI element to store the value of your cool down duration. You can't be sure what happens inside the property when you set it or get it.

avatar image GargoylExtreme · Aug 03, 2015 at 01:55 PM 0
Share

@Nose$$anonymous$$ills: Caught it after the post, but fixed it. Forgot to post it about it. $$anonymous$$y variables are all public, not static. And I type my code by hand more than half the time. I know the horrors of copy/pasting code can create. I also don't have the UI actually storing the cooldown, it just shrinks like a status bar. I just have the bool to indicate when its cooling down and the turrets fire when its false.

@$$anonymous$$acccabbe: I saw a method to use Time.Time, I figured this was the cause, but wasn't sure of a way around it. $$anonymous$$aybe a coroutine or something?

@Nerevar: What setting would allow me to change it through script? Still not totally familiar with the new UI stuff yet.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GargoylExtreme · Aug 04, 2015 at 10:39 AM

Alright, so I ended up solving it myself. Coroutines were the answer. Both turrets now stop with the cool down timer running as expected, and restart the cooldown once repeated as well. Here is the code I used:

     if (activeCD1)
     {
         Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.red;
         Turret1Status.sizeDelta = new Vector2(tur1width,6);
 
         if (tur1width < 1.0f)
         {
             Debug.Log("Recharged");
             StopCoroutine(CoolDown1(44,0,0));
             activeCD1 = false;
         }
 
 
     }
     else
     {
         Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.green;
         Turret1Status.sizeDelta = new Vector3(44, 6);
         tur1width = 44;
     }
 
     public void Turret1Off()
     {
         activeCD1 = true;
         Turret1Active = false;
         StartCoroutine(CoolDown1(44, 0, 10.0f));
     }
 
 
      private IEnumerator CoolDown1(float start, float finish,
  float time)
      {
          if (tur1width <= 0)
              yield return null;
  
          float elapsedTime = 0;
  
          tur1width = start;
  
          while (elapsedTime < time)
          {
              tur1width = Mathf.Lerp(start, finish, (elapsedTime
  / time));
              elapsedTime += Time.deltaTime;
              yield return new WaitForEndOfFrame();
          }
          
      }

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer

Interpolation with jumping - Acting Weird. 1 Answer

FixedUpdate performance doubts 2 Answers

Placing Input.GetkeyDown() inside FixedUpdate() slows down the rate I can fire at 1 Answer

activating multiple voids with the same name trough one line 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