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 Vuzok · Jun 25, 2016 at 11:44 AM · updategetcomponentboolupdate functionupdate problem

C# Update Function Question

I am trying to set a bool component from another C# script from false to true from an update function when the int is greater than a specific number. My problem is that since update calls every frame the bool is constantly made true and can no longer be made false later on. I feel like I've probably missed something super obvious? I think I need to have it in the update function because it needs to be able to check "sphereNo"s value which is always changing. The only time it will go back to false is when I make sphereNo less than 5. Any help would be much appreciated, thanks!

        void Update () {
             if (sphereNo > 5 && popupScript.showPopUp == false) {
                 popupScript.showPopUp = true;
                 popupScript.PopTitle = "WARNING:";
                 popupScript.PopText = "text";
                 popupScript.ToVisit = "sphere2";
             }
            }
         }
Comment
Add comment · Show 3
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 Mindmapfreak · Jun 25, 2016 at 01:00 PM 1
Share

Can you describe the scenario in which you need this?

avatar image JadeTheFlame Mindmapfreak · Jun 25, 2016 at 01:08 PM 0
Share

I agree with ChristAngel here. There are many ways of accessing the script to check "sphereNo", and the most efficient would obviously only be checking when you need to check. Generally if you have changes that occur due to the "sphereNo" value being changed, store it in a function that changes the "popupScript.showPopUp = true; popupScript.PopTitle = "WARNING:";", etc. Then, call that function whenever you change sphereNo. If you let us know what sphereNo is, then we can specify where you would put this check. For instance, if it changes due to a collision somewhere, you put it on the GameObject that collided, perhaps in this case a "sphere"? We need info.

avatar image Vuzok JadeTheFlame · Jun 25, 2016 at 01:44 PM 0
Share

Thank you both for for your help, sphereNo is an integer that increases by 1 every frame in a separate script. I want 'showPopUp to = true when sphereNo's value is greater than 5 so a GUI box will pop up and warn the player. At the moment I have all of my scripts on a script manager gameobject if that makes a difference. I just dont understand how to get sphereNo = true; to be called once when sphereNo passes 5 and then not call it again each frame. Thanks for getting back to me!

2 Replies

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

Answer by JadeTheFlame · Jun 25, 2016 at 01:55 PM

Gotcha! I'm still missing one thing though. If you want it to be called once, why do you need to set it to true again? Does another script rely on it being true?

If your answer is yes, then I have your solution. You need two bools. One bool that the other script relies on, and one that is declared within this script.

PS. "!bool" is the same, and faster than "bool == false". Its good practice ;D

 void Update () 
 {
              if (sphereNo > 5 && !newBoolCalled) 
              {
                  newBoolCalled = true;
                  popupScript.showPopUp = true;
                  popupScript.PopTitle = "WARNING:";
                  popupScript.PopText = "text";
                  popupScript.ToVisit = "sphere2";
              }
 }

If your answer is no. Then the solution is: don't set it to true again.

 void Update () 
 {
              if (sphereNo > 5 && !popupScript.showPopUp) 
              {
                  popupScript.PopTitle = "WARNING:";
                  popupScript.PopText = "text";
                  popupScript.ToVisit = "sphere2";
              }
 }
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 Vuzok · Jun 25, 2016 at 02:27 PM 0
Share

Thanks for all the help, I'm still a little confused. I wasn't trying to set it to true again, it starts off as (false)deactivated and then is set to true for the first time here in the update. I'm a complete noob but I think I was using showPopUp as a bool for the same reason that you used it in the first example? I'm using showPopUp as a bool to activate and deactivate a gameobject which as far as I can tell can't be done through get component. Sorry I should have said this earlier on. So this is in the other 'PopUp' script. 'PopBox' is the GUI element. It seems to activate perfectly my problem is the player needs to click a button to hide the pop up and the update function sort of prevents that. Thanks again for all your help.

         if (showPopUp == true) {
             PopBox.SetActive(true);
         }

PS. Thanks for the !bool tip!!

avatar image JadeTheFlame Vuzok · Jun 25, 2016 at 03:34 PM 0
Share

You can actually deactivate gameobjects in several ways, including referencing the GameObject, and then using objecthere.SetActive(bool). In fact, if you wanted to, you can literally just put your bool key.

     if (objecthere.activeSelf)
     {
         objecthere.SetActive(sphereNo >5);
     }

If you're new to unity, or c#, we should get in touch. I have a my s$$anonymous$$m profile and my email for contact info on my Unity profile page.

avatar image Vuzok JadeTheFlame · Jun 25, 2016 at 11:01 PM 0
Share

I phrased the question really badly. Answer was very simple and you actually answered it the first time around but I thought I had already tried that. For anyone else dumb like me http://stackoverflow.com/questions/31328716/calling-a-function-only-once-from-update-in-different-situations

avatar image
0

Answer by Mindmapfreak · Jun 25, 2016 at 01:55 PM

sphereNo is an integer that increases by 1 every frame in a separate script. I want 'showPopUp to = true when sphereNo's value is greater than 5 so a GUI box will pop up and warn the player. At the moment I have all of my scripts on a script manager gameobject if that makes a difference. I just dont understand how to get sphereNo = true; to be called once when sphereNo passes 5 and then not call it again each frame.

You should probably set showPopUp to true in the script/function where you increment sphereNo, There is no need to check the value every frame if you know exactly where it is incrementing.

Comment
Add comment · Show 2 · 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 Vuzok · Jun 25, 2016 at 02:35 PM 0
Share

I don't fully understand you sorry I'm a noob. Do you mean that it should be in the same place where the sphereNo integer is increasing? I do have it in the same update function. I'm a little confused about what you mean in the last line? As in because I know that if its greater than 5? If I have the same if statement in start though it doesn't know what the current integer value is? Thanks for helping me!

avatar image Mindmapfreak Vuzok · Jun 25, 2016 at 04:57 PM 0
Share

Ah, I think I undestood your problem wrong. You could use an additional bool, which you would set to true when the player clicks on the hide-button. Then you could do something like

 PopBox.SetActive(showPopup && !playerClicked);

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

45 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

Related Questions

Game crashing when im trying to use Inapp update(Unity) 0 Answers

void Update() problem 1 Answer

fixed update is not being called 1 Answer

Loads of errors after upgrading to 2018.3.0f 0 Answers

how's the relationship between updates? 0 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