Script error: OnGUI() can not be a coroutine.....?

Here’s the line of offending code (I think):

	if (GUI.Button (Rect (180,105,130,20), "Plant to House")) {
		var planttohousecam = GameObject.Find ("PlanttoHome Cam");
		planttohousecam.active = true;
		Maincamparent.active = false;
		planttohousecam.animation.Play ("C4D Animation Take");
		yield WaitForSeconds (planttohousecam.animation["C4D Animation Take"].length);
		planttohousecam.active = false;
		Maincamparent.active = true;
		Maincamparent.transform.rotation = Quaternion.identity;
		Maincamparent.AddComponent ("MouseLook");
		}

When the script compiles I get:

What’s happening? Is this not the right way to make it so when I click on a GUI button, it deactivates the current camera, activates new one, plays an animation on that camera, then reactivates the first camera again?

They do that so that you don’t unthinkingly turn it into a coroutine and have a thousand of these running concurrently without realizing it.

The way to get around is to move that functionality into another function call.

function OnGUI() {
. . .
if (GUI.Button (Rect (180,105,130,20), "Plant to House")) { 
Animate();
}
. . .
}
function Animate() {
      var planttohousecam = GameObject.Find ("PlanttoHome Cam"); 
      planttohousecam.active = true; 
      Maincamparent.active = false; 
      planttohousecam.animation.Play ("C4D Animation Take"); 
      yield WaitForSeconds (planttohousecam.animation["C4D Animation Take"].length); 
      planttohousecam.active = false; 
      Maincamparent.active = true; 
      Maincamparent.transform.rotation = Quaternion.identity; 
      Maincamparent.AddComponent ("MouseLook"); 
}

Wow. Cool. Thanks very much for the help. Did just the trick…

It seems that yield cannot stand in the Event Functions directly. It should be encapsulated in a function to achieve so, shouldn’t it?

right, thats why above code works.