Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Update() can not be coroutine...

Discussion in 'Scripting' started by Quist, Oct 24, 2014.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    So i have a script where it says that the "Update() can not be a coroutine".
    And i know what it means but i simply cant find a way to get my script to work else wise :i

    In my second script i have a simple function called *TheCabinetOpens() to play an open animation
    and a function called *TheCabinetCloses() to play a close animation.

    The problem is that the way i tell my first script to know if it should play the open or close animation is by using a var i call "cabinetIsClosed". But i can only have it in the update since else it wont register it...

    Any ideas?

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var hit : RaycastHit;
    4. var Reach : float = 2.0;
    5. var RayHit : boolean;
    6.  
    7. var cabinet : Cabinet;
    8. var cabinetIsClosed = true;
    9.  
    10. private var drawGUI = false;
    11.  
    12.  
    13. function Update ()
    14. {
    15.     var fwd = transform.TransformDirection (Vector3.forward);
    16.     Debug.DrawRay(transform.position, fwd * Reach, Color.red);
    17.     if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "cabinet" && cabinetIsClosed == true)
    18.     {
    19.         RayHit = true;
    20.         cabinet = hit.collider.gameObject.GetComponent("Cabinet");
    21.         drawGUI = true;
    22.         if (Input.GetKeyDown("mouse 0"))
    23.         {
    24.             cabinet.TheCabinetOpens();
    25.             yield WaitForSeconds(0.4);
    26.             cabinetIsClosed = false;
    27.         }
    28.      }
    29.          if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "cabinet" && cabinetIsClosed == false)
    30.     {
    31.         RayHit = true;
    32.         cabinet = hit.collider.gameObject.GetComponent("Cabinet");
    33.         drawGUI = true;
    34.         if (Input.GetKeyDown("mouse 0"))
    35.         {
    36.             cabinet.TheCabinetCloses();
    37.             yield WaitForSeconds(0.4);
    38.             cabinetIsClosed = true;
    39.         }
    40.      }
    41.     else
    42.     {
    43.     RayHit = false;
    44.     drawGUI = false;
    45.     }
    46. }
    47.  
    48. function OnGUI()
    49. {
    50.     if (drawGUI == true)
    51.     {
    52.         GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Click to open");
    53.     }
    54. }
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    You are attempting to use 'Update' as a coroutine since you are using the 'yield' keyword inside the function body.

    I have not tested the following, but hopefully this will help you to see the idea:
    Code (csharp):
    1. #pragma strict
    2.  
    3. var hit : RaycastHit;
    4. var Reach : float = 2.0;
    5. var RayHit : boolean;
    6.  
    7. var cabinet : Cabinet;
    8. var cabinetIsClosed = true;
    9.  
    10. private var drawGUI = false;
    11.  
    12.  
    13. function Update ()
    14. {
    15.     var fwd = transform.TransformDirection (Vector3.forward);
    16.     Debug.DrawRay(transform.position, fwd * Reach, Color.red);
    17.     if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "cabinet" && cabinetIsClosed == true)
    18.     {
    19.         RayHit = true;
    20.         cabinet = hit.collider.gameObject.GetComponent("Cabinet");
    21.         drawGUI = true;
    22.         if (Input.GetKeyDown("mouse 0"))
    23.         {
    24.             StartCoroutine(OpenCabinet());
    25.         }
    26.      }
    27.          if (Physics.Raycast (transform.position, fwd, hit, Reach) && hit.transform.tag == "cabinet" && cabinetIsClosed == false)
    28.     {
    29.         RayHit = true;
    30.         cabinet = hit.collider.gameObject.GetComponent("Cabinet");
    31.         drawGUI = true;
    32.         if (Input.GetKeyDown("mouse 0"))
    33.         {
    34.             StartCoroutine(CloseCabinet());
    35.         }
    36.      }
    37.     else
    38.     {
    39.     RayHit = false;
    40.     drawGUI = false;
    41.     }
    42. }
    43.  
    44. function OpenCabinet()
    45. {
    46.     cabinet.TheCabinetOpens();
    47.     yield WaitForSeconds(0.4);
    48.     cabinetIsClosed = false;
    49. }
    50.  
    51. function CloseCabinet()
    52. {
    53.     cabinet.TheCabinetCloses();
    54.     yield WaitForSeconds(0.4);
    55.     cabinetIsClosed = true;
    56. }
    57.  
    58. function OnGUI()
    59. {
    60.     if (drawGUI == true)
    61.     {
    62.         GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Click to open");
    63.     }
    64. }
     
    Magiichan likes this.
  3. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    You cannot use yield in function Update, you need another function for it.
     
    Magiichan likes this.
  4. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    doesnt work because the CabinetOpens functipn is in a second script and through your script it thinks its in the same script??
     
  5. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Nvm guys i fixed it actually :D

    The problem was that i should remove the WaitForSeconds and set my second opening if state to else if :)