Animation with OnGUI

Hi there,
I’ve got a big problem.
I want an animation start if I click a button but it deosn’t work!!! Hope you can help me!!!
Sorry for bad english…
Script:

var animation_bool : boolean;

function Update ()
{

	if(animation_bool == true)
		{
	
	animation.Play("press");
	
		}

}
function OnGUI () 
{

	if(GUI.Button(Rect(102 , 60, 230, 50), "",GUIStyle.none))
		{
	
		animation_bool = true;
		yield WaitForSeconds(1);
		Application.LoadLevel("MainPanel");
	
		}

}

EDIT:
If I want to start the game the debug window says : Script error: OnGUI() can not be a coroutine.
And if i want to click the button nothing happens

Hi Requid

Remove the yield may solve your problem :wink:

Ok thanks. The scene changes,
but the animation doesn’t start…
Im not that good in scripting and i don’t konw where the mistake is.

I use the animator component and animator controller then in the script: Anim.SetBool, Anim.SetFloat, …

what does the script look then like???

But Anim.SetBool is C#, isn’t it

Some things with your code.

    if(animation_bool == true)

        {
           animation.Play("press");
   
           animation_bool = false;   
        }

For the other, look at StartCoroutine() in the script reference.
Anim.SetBool() could be either language.

As I said before: I prefer use mecanim system and the code maybe look like this

In Java Script:

private var switchStatus : Boolean = false;
private var anim : Animator;
function Update()
{
     anim.SetBool("Bool_In_AnimatorController", switchStatus ); // Bool_In_AnimatorController must create in Animator Controller
}
OnGUI()
{
     if(GUI.Button(new Rect(100,100,100,100), "Status: " + switchStatus))
     {
          if(switchStatus)
          {
               switchStatus  = false;
          }
          else
          {
                switchStatus  = true;
          }
     }
}

I’m not Good In English :-p :wink: