Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

yield WaitForSeconds (0.1) not working in Unity 5?

Discussion in 'Scripting' started by kerrmedia, Feb 18, 2016.

  1. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    Using Unity 5.2.2.1f.

    following code sample:

    yield WaitForSeconds (0.1);

    Error:
    Error 1 The type or namespace name 'yield' could not be found (are you missing a using directive or an assembly reference?)

    But it was in the intellesense drop down.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    What's the context you're using it in? IE - what is the code surrounding that statement.
     
  3. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    Weird, just cut and pasted the example from the docs and changed the name, now it compiles. Hmmmm.




    Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    using UnityEngine;
    using System.Collections;

    public class WaitForSecondsExample : MonoBehaviour {

    void Start() {
    StartCoroutine(Example());
    }

    IEnumerator Example() {
    print(Time.time);
    yield return new WaitForSeconds(5);
    print(Time.time);
    }

    }
     
    Last edited: Feb 18, 2016
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That's a different error, but whatever.

    It's throwing that error in that file? What line? Also - please wrap your code in code tags so it's easier to read.
     
  5. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    No errors, but not pausing the script execution. Thank you for your help.
    I'm assuming that I'm not seeing the color change because the Update keeps firing and setting the color back even with the StartCoroutine?

    Code (CSharp):
    1.  
    2. // Update is called once per frame
    3.    void Update ()
    4.   {
    5.             // if lest mouse button is clicked
    6.           // create a raycast that originate from the mouse clicked position
    7.  
    8.  
    9.          if(Input.GetMouseButtonDown(0))
    10.          {
    11.                  Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.                   RaycastHit hitInfo;
    13.                  var fwd = transform.TransformDirection(Vector3.forward);
    14.                  if(Physics.Raycast(rayOrigin,  out hitInfo, distance))
    15.                  {
    16.                         Debug.Log("Your are casting a ray");
    17.  
    18.                          Debug.DrawRay(rayOrigin.direction, hitInfo.point, Color.green);
    19.  
    20.                        if (hitInfo.rigidbody != null)
    21.                          {
    22.                                  Debug.Log("applying force");
    23.                                   AddHighlight(hitInfo.collider.gameObject);
    24.                                  hitInfo.rigidbody.AddForceAtPosition(rayOrigin.direction * power, hitInfo.point);
    25.                          }
    26.                           else
    27.                          {
    28.                                   Debug.Log("null?");
    29.                          }
    30.                 }
    31.           }
    32.    }
    33.  
    34.   IEnumerator WaitThreeSeconds()
    35.   {
    36.           print(Time.time);
    37.          yield return new WaitForSeconds(5);
    38.           print(Time.time);
    39.   }
    40.  
    41.  
    42.   void AddHighlight(GameObject gameObj)
    43.   {
    44.          Color color = gameObj.GetComponent<Renderer>().material.color;
    45.          color.r = color.r * 10;
    46.           color.g = color.g * 10;
    47.           color.b = color.b * 10;
    48.           gameObj.GetComponent<Renderer>().material.color = color;
    49.  
    50.           StartCoroutine("WaitThreeSeconds");
    51.  
    52.           color = gameObj.GetComponent<Renderer>().material.color;
    53.           color.r = color.r / 10;
    54.           color.g = color.g / 10;
    55.           color.b = color.b / 10;
    56.           gameObj.GetComponent<Renderer>().material.color = color;
    57.  
    58.   }
     
    Last edited: Feb 18, 2016
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Again - code tags: http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    It's pretty hard to provide any guidance since the error you're getting has been different every time you've posted and now you're apparently not getting any errors at all. You also mentioned changing color in a coroutine but your coroutine doesn't change the color of anything. StartCoroutine doesn't actually make the execution pause which is why the code after it is executing immediately. If you want to wait before changing it back then AddHighlight has to be a coroutine.
     
  7. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    OK, sorry about the missing tags, first time really posting here. Trying to work with code examples that have been changed by newer Unity3D API I'm thinking.
    I appreciate your help. I guess setting the color back to normal would be called from within the "WaitThreeSeconds" coroutine?
    really just trying to briefly highlight an object when the user passes over it.
     
  8. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Indeed, the code after
    Code (csharp):
    1. StartCoroutine("WaitThreeSeconds");
    executes instantly because it's not inside the coroutine.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The other issue is that this will execute every frame your mouse is on the object so you're spawning a bunch of coroutines. You'll need a boolean or something to maintain some state about whether or not the coroutine is running.

    And your fwd Vector3 could just be this:
    Code (csharp):
    1.  
    2. var fwd = transform.forward;
    3.  
    to skip the TransformDirection call.
     
  10. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    Wonderful, working now. I'll add the check so I don't call the same co-routine for the same object while the timeout is still executing.
    Thank you everyone!
    Scott