IEnumerator coroutines crashing app when launched with datatyle "GameObject" passed in

Hi guys,

I have a game where there is a claw that is supposed to open and close its claws when a particular button is tapped (which posts a notification). The following is the code from CraneBehavior.cs that launches the relevant coroutines:

	void OpenClaw()
	{
		StartCoroutine("OpenClaw", craneClaw1);
		StartCoroutine("OpenClaw", craneClaw2);
		StartCoroutine("OpenClaw", craneClaw3);
		StartCoroutine("OpenClaw", craneClaw4);
	}
	
	void CloseClaw()
	{
		StartCoroutine("CloseClaw", craneClaw1);
		StartCoroutine("CloseClaw", craneClaw2);
		StartCoroutine("CloseClaw", craneClaw3);
		StartCoroutine("CloseClaw", craneClaw4);
	}
	
	IEnumerator OpenClaw (GameObject claw)
	{
	    Vector3 angles = transform.eulerAngles;
		float startTime = Time.time;
		float startAngle = angles.y;
		float endAngle = startAngle + 45; // -45 for left turn
		
		while(duration >= Time.time - startTime)
		{
		    angles.y = Mathf.LerpAngle(startAngle,endAngle,(Time.time - startTime) / duration);
		    transform.eulerAngles = angles;
		    yield return null;
		}
	}
	
	IEnumerator CloseClaw (GameObject claw)
	{
	    Vector3 angles = transform.eulerAngles;
		
		float startTime = Time.time;
		
		float startAngle = angles.y;
		float endAngle = startAngle - 45; // -45 for left turn
		
		while(duration >= Time.time - startTime)
		{
		    angles.y = Mathf.LerpAngle(startAngle,endAngle,(Time.time - startTime) / duration);
		    transform.eulerAngles = angles;
		    yield return null;
		}
	}

OpenClaw() and CloseClaw(), which start coroutines that open and close the claw, cause the app to crash when called (When testing the app in the editor, the editor/player freezes without even printing an error. When built to my phone, it gets a bad notification in Xcode). I suspect that the crashes are being caused by the coroutine code, since this problem was nonexistent when TogglePlayMode was present but did not call the relevant coroutines to open and close the claw. Both OpenClaw() and CloseClaw() are called in the following bit of code in the same file. It responds to a notification posted to the wiki’s NotificationCenter.cs shared notification center:

void TogglePlayMode()
	{
		// This opens and closes the claw
		if (clawOpen)
		{
			clawOpen = false;
			// TODO: Close the claw by sending a rotate message
			
			CloseClaw();
		}
		else
		{
			clawOpen = true;
			// TODO: Open the claw by sending a rotate message
			
			OpenClaw();
			
		}
	
	}

What is wrong with my coroutine implementation, if my hypothesis is correct?

MachCUBED

I didn’t think this would cause it to crash, but it looks like you’re just modifying the eulerangles of whatever transform the CraneBehavious script is attached to. You pass the claw objects into the Open and Close coroutines but never reference them. “transform.eulerAngles” accesses the transform that the script is attached to. To rotate the claws you need “claw.transform.eulerAngles”.

I haven’t checked if this fixes the crash, but the rest of the script seems legit.

I’m pretty sure that your problem comes from the combination of those points:

  • using the string version of StartCoroutine
  • Having two functions named “OpenClaw”
  • Starting OpenClaw inside OpenClaw.

I strongly recommend to use a different name for the coroutine. Also it’s better for performance to start the coroutine like this:

    StartCoroutine(OpenClaw(craneClaw1));

This would already fix your problem i guess.