Unity 5.6 freezes for unknown reason

Hi Everyone,

For some unknown reason unity freezes when using a coroutine. It loops well for one cycle but then unity freezes. The script I used always worked well so I really have no clue what is going wrong here.

short explanation:
The script starts at getSprites() method, then it goes to setUnitQQ() method. In that method the coroutine delayer is activated. Total buildtime (buildT) = 3 and every second that value decreases by 1. When buildT reaches 2 the script freezes.

If anyone could help me out that would be hugely appreciated.

void setUnitQQ() //test what unit queque nr this warfactory has to use
    {


        StartCoroutine ("Delayer");
    }
      

       void UnitBuild()
    {
        if (unitName.Count > 0) {
        if (puCredits == 1) {
            if (P1game.creditsUp1 >= unitPrice [0]) { //unit price
                    Debug.Log(unitPrice[0] + " creditsUp1 " + P1game.creditsUp1);
                    P1game.creditsUp1 -= unitPrice [0];
                buildEnable = false;
            }
        }
        else
        {
            if (P2game.creditsUp2 >= unitPrice [0]) { //unit price
                P2game.creditsUp2 -= unitPrice [0];
                buildEnable = false;
         
            }
          }
        }
 
     }


      void BuildTimer()
    {
        buildT -= 1;
        if (buildT <= 0)
        {
            //SpawnUnit (); //unity freezes before this method can be excuted so I left it out
            buildT = 3; //reset buildT
        }
    }



         IEnumerator Delayer() //buildtime delayer
        {
            while (true) {
            yield return new WaitForSeconds (1);
            if (buildEnable) {
                UnitBuild ();
            }
            else
                {
                BuildTimer ();
                }

            }
         }


    public void getSprites()
    {
        setUnitQQ(); 
    }

}

I’m really not sure if this code is the problem. I stripped it down to this:

    int buildT = 3;

    void Start()
    {
        StartCoroutine("Delayer");
    }

    IEnumerator Delayer()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            BuildTimer();
        }
    }

    void BuildTimer()
    {
        Debug.Log(buildT);
        buildT -= 1;
        if (buildT <= 0)
            buildT = 3;
    }

and it works without any kind of problems. It just counts down 3-2-1 and then repeats that.
If that works for you the problem will probably be elsewhere.

Two things to note. With coroutines, you should use StartCoroutine(Delayer()); instead of the string version. The string version is actually slower.
If you have to use a yield return new WaitForSeconds it’s best to create the variable
WaitForSeconds wfs = new WaitForSeconds(1f); for example and then just use the wfs so you don’t create a new one in your coroutine.

Now, for your issue, it’s not likely your coroutine is the source of the problem. Usually freezes are created because of infinite loops (this is normally the reason). Because coroutines are designed to yield, they generally don’t create the freeze. So you’ll need to look into what else is going on within your other scripts.

Thanks for your reply. Yeah thats basically what I have right now and it always worked. Is there any way to find out why Unity freezes ?

Should I reinstall ?

What do you mean that is what you have? Reinstalling isn’t going to do a thing if it’s your script. It’s not likely an issue with Unity.

Well I checked for other scripts but when I disable the script I posted Unity won’t freeze. So my guess is that the problem should be somewhere within the code of that script. However if the script should be working and it isn’t then I don’t know what else can be the case.

Thanks for the advice on Coroutines I’ll adjust my script right away.

If that script changes something that another script uses, it could also create a freeze. But if you can disable it and not get a freeze, then it’s more than likely a script issue. I couldn’t tell you what is wrong because off hand I don’t see anything that could make it freeze from what you posted.

I’d be throwing Debug.Log lines everywhere to figure out how far you’re getting before it “freezes”, also in any other script that has anything to do with this one.

What I remember is that I got a message before unity frooze asking of I would let unity change some stuff. Because everything was working fine up to that point I neglected it and cancelled. Is there a way I can let unity somehow re-scan my project so it will find the same errors ?

It makes one loop in buildT, buildT starts at 3 and when unity freezes it’s 2.

Could we see the full script perhaps?

Depending on what version you upgraded from, it may or may not create issues. I think you can delete the library folder to have it reimport. I always create a 7zip backup of my projects before upgrading or doing major changes. You could also try to reimport all in Unity and see if that helps.

Thanks for all the support guys I really appreciate it.

So I just executed the script I posted without other scripts working and the loop works perfectly. Also I checked other scripts that had loops in them and they all worked well. For some reason the script posted makes unity freeze when all the scripts in my project are enabled.

After I made a zip file backup I deleted my library folder. How do I re-import it ?

Deleting the library folder will cause a reimport. Just launch unity.

Yeah but the objects in my scene are gone. Though I have a recent backup of my project so it doesn’t matter that much.

Is your project not set to Text serialization? That shouldn’t happen.

Where can I adjust these settings ?

Edit → Project Settings → Editor → Asset Serialization

Hey guys so I found the error. It was a loop in SpawnUnitG script but the weird thing is that it was executed before my debug.log statement was triggered a method before. Apparently loops are being executed before even the code has reached them. Anyway I used x as variable in my loop but I added i++ so yeah it was a loop that caused the freeze but the loop was long after my debug.log statements so that’s weird.

Just curious but does anyone have an idea why loops are being executed before the code even reaches them ?

Thanks everyone for your help !

It would help if we could actually see the code of that script.