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

help required for Wait For Seconds / yield

Discussion in 'Scripting' started by BlueOnE, Feb 2, 2015.

  1. BlueOnE

    BlueOnE

    Joined:
    Jan 24, 2015
    Posts:
    16
    Hi,
    so far I've been pretty decent doing c# code in Unity, but this 1 stupid function WaitForSeconds, I cannot get to work, been trying this close to 2 hours now...
    After searching the forums and google, I still can't figure out what's the problem.
    I have the following piece of code, which is giving the following error: "Parser Error: a get or set accessor expected".
    In all fairness I have no clue what a yield does at all, but I hope someone can help me understand but mostly fix this.

    Because it's a long class, here's the 2 functions that are related to the problem:
    Code (CSharp):
    1.     public void pauseWalking(){
    2.                 stopWalking ();
    3.         StartCoroutine("continueWalking");
    4.  
    5.         }
    6.     public IEnumerator continueWalking{
    7.         yield return new WaitForSeconds(3f);
    8.         startWalking ();
    9.     }
    Any information is much appreciated, thanks so far.
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    There's more to the error message, and your code, than you're giving us. Don't expect magical solutions from a tiny piece of an error message and 9 lines of a "very long" script.

    And my favorite advice:
    If you don't know what a particular line of code does, you shouldn't be using it.

    There is no shortage of documentation on the yield keyword in the world of Google.
     
  3. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Yield (hold on there lumpy) WaitForSeconds(wait this long before you continue).
    As far as your error I am not sure but that is pretty much what Yield WaitForSeconds does.


    is stopWalking another function or a variable?
    if it is a variable I would think that the () after it instead of ; is the problem/
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    Your continueWalking function is lacking open/close parentheses.
     
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    @Kurt Dekker is right - you need to put a ()

    If it still does not fix the error, can you post whats there in startWalking ?
     
  6. DP00707

    DP00707

    Joined:
    Aug 13, 2014
    Posts:
    29
    To expand on that (not sure if it will work, try:

    Code (CSharp):
    1.    public void pauseWalking(){
    2.                 stopWalking ();
    3.         StartCoroutine(continueWalking());
    4.         }
    5.     public IEnumerator continueWalking(){
    6.         yield return new WaitForSeconds(3f);
    7.         startWalking ();
    8.     }
     
  7. BlueOnE

    BlueOnE

    Joined:
    Jan 24, 2015
    Posts:
    16
    omg, *insert many very bad cursewords here*, how did I miss that.
    Guess I've been staring at it for to long at 2am :(
    Many thanks to all though :).
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Just to clarify, StartCoroutine(string methodname) is still a valid function, and has tradeoffs with StartCoroutine(IEnumerator routine). It should have still worked had you kept the inital continueWalking call in quotes - you just needed to place the parenthesis around the actual method implementation.