Unity crashing when Coroutine called

Unity crashes when this code runs:

private IEnumerator beam(int count,GameObject proj,Vector3 location,bool isEnemy, int damage)
    {
        Debug.Log("beam start");
        for(int i=0;i<count;i++)
        {
            ob=Instantiate(proj,location,Quaternion.identity);
            pr=ob.GetComponent<projectile>();
            pr.damage=damage;
            yield return new WaitForSeconds(0.1f);
        }
        Debug.Log("beam pause");
        yield return new WaitForSeconds(2);
        hasThree=false;
        if(isEnemy)
        {
            turn++;
        }
        Debug.Log("beam end");
      
    }
    private IEnumerator approach(int count,GameObject proj,Vector3 location,bool isEnemy, int damage)
    {
        Debug.Log("approaching");
        float currentApproach=0;
        while(Input.GetAxis("Fire1")==0){
            yield return new WaitForSeconds(0.1f);
            Debug.Log("waiting for J");
        }
        while(Input.GetAxis("Fire1")==1 && currentApproach<maxApproach)
        {
            battleMax.transform.position+=Vector3.right*0.1f;
            currentApproach+=0.1f;
            yield return new WaitForSeconds(0.05f);
            Debug.Log("Waiting for J (2)");
        }
        Debug.Log("Creating Beam");
        yield return StartCoroutine(beam(count,proj,location,isEnemy,damage));
        Debug.Log("Beam Done");
        while(battleMax.transform.position!=maxPos)
        {
            battleMax.transform.position+=(battleMax.transform.position-maxPos)/6;
            Debug.Log("Returning");
        }
        battleMax.transform.position=maxPos;
        Debug.Log("Done");
        mode=0;
    }

I have it call approach, which calls beam, and it crashes when the beam is instantiated.
It could be that I am on a crappy computer, but I just wanted to see if anyone else had/could fix this issue.
I can provide any context if needed.

As far as I can see, the code doesn’t compile. The line ob=Instantiate(proj,location,Quaternion.identity); uses the variable “ob” which is not declared. You need to write “var” in front of the line to make it compile. (note: I may be wrong if “ob” is a member variable. But this would be very bad code because it doesn’t need to be a member and if it were a member, it should have a better name)

So, if the code doesn’t compile, the last working version of your code is executed. The crash may be something you already fixed or a totally different section of the code causes it.
You should regularly check the unity console for compile errors.

(note: I stopped reading your code after seeing the Syntax error, so maybe there is still something triggering a stack overflow in there)

Apart and aside from compileability, your while() loop at line 39 does not have a yield in it and will either a) execute all in one frame, or b) lock up the computer, depending on whether the end condition can ever be true.

Also: don’t test floating point quantities (scalar or vector) for equality, generally speaking, unless you are prepared to deal with them never quite being equal.

3 Likes

ob is declared right before beam. It’s a private GameObject.