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";
}
}
}
Can you describe the scenario in which you need this?
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.
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!
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";
}
}
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!!
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.
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
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.
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!
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
Follow this Question
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