- Home /
WaitForSeconds using Update?
The script has no complier errors before the game starts. However, once the game starts, I get an error that says "Update () cannot be used as a coroutine". I've searched the web for a couple hours now, but I can't seem to find a way to re-write my script, and make it work. I've re-written it a couple times, but I'm stuck as to what to do right now. If anybody could help me solve this, it would be much appreciated. Thanks in advance.
     var jumpslash : AnimationClip;
     
     var doingsomethingelse : boolean = false;
     
     function Update ()
     {
     if(doingsomethingelse==true)
      {
     yield WaitForSeconds (2.0);
     doingsomethingelse = false;
     }
     
     if(Input.GetKeyUp(KeyCode.F))
     {
     if(doingsomethingelse==false)
     {
     animation.Play("jumpslash", PlayMode.StopAll);
     doingsomethingelse = true;
          }
     
      }
     
     }
Answer by Landern · Jun 30, 2015 at 01:13 AM
First, a google search for "Update () cannot be used as a coroutine" yielded(heh) the answer in the first two results.
You can't use yield in update in UnityScript.
Move your call into another function
 var jumpslash : AnimationClip;
 var doingsomethingelse : boolean = false;
 
 function Update ()
 {
     if(doingsomethingelse==true)
     {
         WaitASec();
     }
 
     if(Input.GetKeyUp(KeyCode.F))
     {
         if(doingsomethingelse==false)
         {
             animation.Play("jumpslash", PlayMode.StopAll);
             doingsomethingelse = true;
         }
 
     }
 }
 
 function WaitASec ()
 {
     yield WaitForSeconds (2.0);
     doingsomethingelse = false;
 }
The animation plays, but the boolean does not check off as true. I rearranged a line or two (literally), and I got this
  var jumpslash : AnimationClip;
  var doingsomethingelse : boolean = false;
  
  function Update ()
  {
      if(doingsomethingelse==true)
      {
          WaitASec();
      }
  
      if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.F))
      {
          if(doingsomethingelse==false)
          {
              animation.Play("jumpslash", Play$$anonymous$$ode.StopAll);
              doingsomethingelse = true;
          }
  
      }
  }
  
  function WaitASec ()
  {
      yield WaitForSeconds (2.0);
      doingsomethingelse = false;
  }
It now works perfectly. Thanks for your help!
@SpartanSperos, ah i missed that, if it's all go, accept the answer so others can benefit
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                