- Home /
calling a void twice makes it run for 2 frames
I have a void that is called multiple times during runtime, but it only works the way it should the first time it's called. The code:
waitInt += 1;
if (waitInt > 160)
{
The first time I call the void it lets waitInt get to 160, but the second and third time it only lets waitInt get to 2. Anyone know why?
I have figured it out. I said screw it just did this ins$$anonymous$$d:
void bothTeamCheck()
{
if (($$anonymous$$m2Score > maxScore - 1 || $$anonymous$$m1Score > maxScore - 1) && ($$anonymous$$m1Score - $$anonymous$$m2Score >= winByValue || $$anonymous$$m2Score - $$anonymous$$m1Score >= winByValue))
{
torf = true;
print("number of frames");
}
}
and
void waitTimer ()
{
if(torf == true) {
seconds += Time.deltaTime;
if (seconds > 3)
{
torf = false;
e = 0;
seconds = 0;
i know its not the correct way to do it but i needed a quick fix.
Glad you figured it out. Like I mentioned before, this thing you were attempting though is really the bread and butter of coroutines. That'd be something I'd look at when you need to do something like this in the future. $$anonymous$$y theory though is "if it ain't broke, don't fix it" (unless you think it might break later, or slow things down)... so you have things working.. no point in trying to fix something that works!
Take care.
Answer by cstooch · Jun 22, 2017 at 04:57 PM
Weird pet peeve, but people use the term "a void" in a way they probably shouldn't. Void just means it is a function that does not return a value. Any function can have void at the beginning.. assuming you don't want it to return a value (i.e. it's not just Update, Awake, Start, etc.). But anyway, I digress..
So, on to your problem... you didn't supply enough code for someone to see why this didn't work, so please supply the code for where the function is called, plus the full code of this function, plus where you declare waitInt is probably a good idea as well.
void bothTeamCheck()
{
if (($$anonymous$$m2Score >= maxScore | $$anonymous$$m1Score >= maxScore) && ($$anonymous$$m1Score - $$anonymous$$m2Score >= winByValue | $$anonymous$$m2Score - $$anonymous$$m1Score >= winByValue))
{
waitInt += 1;
if (waitInt > 160)
{
e = 0;
waitInt = 0;
is in the update function
void Update () {
bothTeamCheck();
well, now that you've sent code in, I'm even more baffled because the issue you're saying doesn't even make sense with the code you have: "The first time I call the void it lets waitInt get to 160, but the second and third time it only lets waitInt get to 2. Anyone know why?"
I assume when you say "first time" / "second and third time" you're talking about frames (i.e. subsequent updates). The thing is you aren't looping at all, that I see, and that code that you pasted for "bothTeamCheck" would only happen once per frame. That means that the first time through, your waitInt should be 1. 2nd time, it would be 2, 3rd time it would be 3.
Now, if you set waitInt somewhere else outside of this, like the inspector.. that might be different. For example, if you set waitInt to 160 in the inspector, the first time through it would change to 161 from the increment, and then, reset to 0 in your if condition. Next pass through, it would be 1... 3rd time through, it would be 2.
Do you understand why this comment makes no sense now? "The first time I call the void it lets waitInt get to 160" Unless you're calling bothTeamCheck a bunch of times elsewhere, or incrementing / setting waitInt somewhere else, that statement doesn't really make sense. With what you did, the first frame should have waitInt = 1 with this code.
An update statement is called once per frame. If statements do not loop (unless of course you put them inside of a loop). What you actually see$$anonymous$$gly want to happen (for this to have waitInt increment 160 times, by the looks of it??) isn't going to happen because of what you coded.
@cstooch I made a video showing my problem.
As you can see, when the score reaches a certain max score, it waits till waitInt is greater than 160 then switches sides and sets the scores to 0. But on the second time, it only runs the update for 2 frames thus letting waitInt only get to two.
this is problematic:
($$anonymous$$m2Score >= maxScore | $$anonymous$$m1Score >= maxScore) && ($$anonymous$$m1Score - $$anonymous$$m2Score >= winByValue | $$anonymous$$m2Score - $$anonymous$$m1Score >= winByValue)
first of all, you are doing bitwise comparisons here when you want to be doing logical comparisons. change your single pipes to double:
($$anonymous$$m2Score >= maxScore || $$anonymous$$m1Score >= maxScore) && ($$anonymous$$m1Score - $$anonymous$$m2Score >= winByValue || $$anonymous$$m2Score - $$anonymous$$m1Score >= winByValue)
@habitablaba doesn't that mean and not or?
Answer by piecewise13 · Jun 22, 2017 at 08:47 PM
@Habitablaba and @cstooch
void bothTeamCheck()
{
if ((team2Score >= maxScore || team1Score >= maxScore) && (team1Score - team2Score >= winByValue || team2Score - team1Score >= winByValue))
{
print("number of frames");
waitInt += 1;
if (waitInt > 160)
{
e = 0;
waitInt = 0;
print("yea");
team1Score = 0;
team2Score = 0;
team2TimeoutsCalled = 0;
team1timeoutsCalled = 0;
if (switchSidesToggle.isOn)
{
if (isLeft == true)
{
isLeft = false;
team1Button.position = rightButtonPos.position;
team1TimeoutTrans.position = rightTimeoutPos.position;
team1SetPos.position = rightSetPos.position;
team1NameCh.position = rightName.position;
team1Color.position = rightColor.position;
team2Button.position = leftButtonPos.position;
team2TimeoutTrans.position = leftTimeoutPos.position;
team2SetPos.position = leftSetPos.position;
team2NameCh.position = leftName.position;
team2Color.position = leftColor.position;
}
else
{
isLeft = true;
team1TimeoutTrans.position = leftTimeoutPos.position;
team1Button.position = leftButtonPos.position;
team1SetPos.position = leftSetPos.position;
team1NameCh.position = leftName.position;
team1Color.position = leftColor.position;
team2Button.position = rightButtonPos.position;
team2TimeoutTrans.position = rightTimeoutPos.position;
team2SetPos.position = rightSetPos.position;
team2NameCh.position = rightName.position;
team2Color.position = rightColor.position;
}
}
}
}
}
What the values are for: team1Score and team2Score increase by one everytime the buttons are pressed. maxScore is controlled by a slider. winByValue are changed by a slider that the play can change and if team1 and team2 score are greater then win by and maxScore, it equales true and makes it run.
Nothing jumps out at me just yet, but one thing I'll point out is you appear to be using 160 frames as a length of time to pause. Just so you know the time it takes to display a frame isn't always constant. You might want to look at coroutines for "pausing" your game, or even just simply using a counter based on game time. But that isn't really what's causing your problem here.
Is there something that destroys this script, perhaps?
I'd add an else branch to your main if statement, and just put any Debug.Log there, and just make sure that something is happening once the count hits 2 and stops. If you aren't seeing that log output, then that would probably suggest the game object this script is attached to got destroyed.
You might even want to print out the waitInt value somewhere too (ideally to your game UI, so your console isn't spammed, but not 100% necessary).
I did print out the waitInt but it had the same problem, it would only add 2 on the second try but ill add the else statement and see if that makes a difference.
Yeah an if on that will tell us a lot here...
If you see debug log statements, that means that the logic in your if statement is not resulting in 'true'.. (then I'd be printing out these variable values individually in the else statement to get a sense as to why this isn't returning true)
If you DONT see debug log statements, I would think that means that your object that has the update is destroyed or disabled, probably
I added the else statement, but what's weird is its printing the else statement one time less than the main function one the first time (161) and twice on the second one.
Sorry, by the sounds of that, I think you put the else on the " if (waitInt > 160)" part (is that right?). I was thinking of having it on the "if (($$anonymous$$m2Score >= maxScore || $$anonymous$$m1Score "etc, part. You could actually even just put output right as the first line of the bothTeamCheck, even. It would spam your console/log a bit (but you've got it collapsing anyways.. just put something in there that would collapse).
Anyways, here's why I'm suggesting this... I have a feeling that either your if statement isn't evaluating to true when your game ends the second time, for some reason, or something has destroyed your object where this script is attached, and that would help deter$$anonymous$$e that. Really, I think it's just going to be a bit of doing debug.log output here, and there to figure out at what point the part with " print("number of frames");" no longer is being reached - is it no longer being reached because the if statement before it no longer evaluates to true? Or is it because this game object is destroyed/disabled?
Are you doing anything to destroy/disabled the game object this attached to that you can think of? Do you still see it in the hierarchy when the game appears to stop incrementing waitInt?
Your answer
Follow this Question
Related Questions
why is every 2. frame is weird with Time.deltaTime 1 Answer
Boolean Trouble, Please help....Boolean does not flip another boolean 0 Answers
Play method only ONCE when detected by the update function 3 Answers
how to stop ui button jitter if it has to be called on update 0 Answers
Character,Camera,Updates,Frame Rates,Jitter&Stutter, I need some explanations. 0 Answers