PC crashed in while loop, cant see whats wrong

Hi guys, so I checked the unity crash log and it said it crashed while doing a while loop at line 10, I cant see any problem, am I missing something?

IEnumerator Refill(){

        while (refilling){

            if(HJ.ATT.ShotsLeft <= HJ.ATT.AllowedShots){

                HJ.ATT.ShotsLeft += 1;
                RefillSlider.value = HJ.ATT.ShotsLeft / HJ.ATT.AllowedShots;

                print("Refilling");
            }

            if(HJ.ATT.ShotsLeft >= HJ.ATT.AllowedShots){

                HJ.ATT.ReachedAS = false;
                refilling = false;

                print("Finished refilling");

                if(HJ.isEnemy){
                    returnToBase = false;

                }

                if(HJ.ATT.foundTarget){
                    HJ.ATT.foundTarget = myTarget;
                }

                if(HJ.isEnemy){

                    if(!myTarget && ESC.GoodBuildings.Count > 0){

                        GoGetEm();

                    }
                }
            }

            yield return new WaitForSeconds (RefillTime);
        }

    }

Hard to say. What is calling the routine? Are there setters on the hj.att method?

No setters, only references to them. It is being called in another while loop, which does get set to false.

But it is weird now, after 20 mins it crashed from something else, nothing to do with this code, last line run from unity crash log was an Ontrigger function, which is fine.

So it is something else causing it but I don’t know what.

The reason I ask is because the value that is being checked is technically outside the loop. You are changing the value on another object then asking that other object of the value later. It may not be your problem at all, but is a good place to look. If there is anything in that object that alters that value (like a getter or setter). It might affect things.

What does the loop look like that is calling the refill ?

IEnumerator WaitForJetTurn(){

        while (isLanding){
           
            if(returningHome && isLanding){

                transform.rotation = Quaternion.Slerp(transform.rotation, OriginalRot, Time.deltaTime * dampingRotation);
                transform.rotation = Quaternion.Euler(new Vector3(0f, transform.rotation.eulerAngles.y, 0f));

                transform.Translate(0, -SlowSpeed * Time.deltaTime, 0);

                if(transform.position.y <= basePosition.position.y){

                    isLanding = false;
                    returningHome = false;

                    HaveClickTarget = false;
                    IHaveLanded = true;

                    ReturnToFP = false;
                    ReturnToFP1 = false;

                    if(HJ.isEnemy){
                        FlyingToFPE = true;
                    }

                    spinBlades = false;

                    if(Jet3){
                        Jet3AtFTP = false;
                        GoingBack2Base = false;
                        GameController.Jet3CanBeSelected = true;
                    }

                    if(!Jet3){ // If not Jet Transporter.
                       
                        myTarget = null;

                        Att.targets.Clear();

                        refilling = true;
                        StartCoroutine (Refill());

                        print("Start the refill");
                    }

                    for (int i=0; i < Boosters.Length; i++){

                        if(Boosters.Length > 0){

                            Boosters[i].enableEmission = false;

                        }
                    }

                    if(HJ.isEnemy){
                       
                        if(!HJ.ATT.ReachedAS && !myTarget && ESC.GoodBuildings.Count > 0){

                            GoGetEm();

                        }
                    }
                }
            }

            yield return new WaitForSeconds (0.01f);
        }
    }

Yea, it doesn’t appear to be an infinite loop issue.

You are probably correct that something else is causing it, and it just happened that it crashed during that method returning the last thing it did.

Yeah, thanks for having a look though, thought I might of missed something simple, but nope :slight_smile: I think it may be an issue with a * pathfinding, so I made some tweaks and I got the game to run for like 40 mins, enemy killed me and no crash! so far…

might be nothing, but your logic allows for the “shotsleft” value to exceed the “allowedshots”

OP line 5

if(HJ.ATT.ShotsLeft<= HJ.ATT.AllowedShots) {...}

“If I’m below or at the limit, add another one.” Maybe this is causing issues elsewhere?

1 Like

hey thx @LeftRighty, I see what you mean and it makes sense. I have now removed equal to from both statements.