Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

yield return new WaitForSeconds C# help

Discussion in 'Scripting' started by ployer, Aug 21, 2011.

  1. ployer

    ployer

    Joined:
    Jul 28, 2009
    Posts:
    41
    I am translating some code from javascript to C# in a book im reading and ive across slight issue. When i translate this into C# it gives me the error


    Error 1 The body of 'TileGenerator.MatchCheck()' cannot be an iterator block because 'void' is not an iterator interface type C:\Users\Phil\Documents\MatchGame\Assets\_scripts\TileGenerator.cs 121 10 Assembly-CSharp-vs

    So obviously i cant do this in C# how would i achieve the same affect?

    Thanks, Phil
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    329
    You kind of didn't paste the code, but I have a feeling from the error you're getting the coroutine is

    Code (csharp):
    1. void COROUTINE()
    2. {
    3.     yield return new WaitForSeconds(x);
    4. }
    instead of

    Code (csharp):
    1. IEnumerator COROUTINE()
    2. {
    3.     yield return new WaitForSeconds(x);
    4. }
     
  3. ployer

    ployer

    Joined:
    Jul 28, 2009
    Posts:
    41
    Here is my code

     
  4. scarpelius

    scarpelius

    Joined:
    Aug 19, 2007
    Posts:
    951
    Code (csharp):
    1.  
    2. [COLOR="red"]IEnumerator [/COLOR]MatchCheck()
    3. {
    4. ...
    5.  
    maybe like this?
     
  5. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,685
    Code (csharp):
    1. IEnumerator MatchCheck() {
    2.     if (tName1[0] == tName2[0]) {
    3.         canClick = false;
    4.         yield return new WaitForSeconds(2);
    5.         Destroy(matchOne);
    6.         Destroy(matchTwo);
    7.         canClick = true;
    8.         numberOfTiles = numberOfTiles - 2;
    9.     }
    10.     if (numberOfTiles == 0) {
    11.         print("End Game");
    12.     } else {
    13.         canClick = false;
    14.         yield return new WaitForSeconds(2);
    15.         print("Rotate Back");
    16.         matchOne.transform.Rotate(new Vector3(0, -180f, 0));
    17.         matchTwo.transform.Rotate(new Vector3(0, -180f, 0));
    18.         canClick = true;
    19.     }
    20.     matchOne = null;
    21.     matchTwo = null;
    22. }
     
  6. ployer

    ployer

    Joined:
    Jul 28, 2009
    Posts:
    41
    thanks all that seemed to work!
     
  7. masterofyngwie

    masterofyngwie

    Joined:
    May 14, 2012
    Posts:
    9
    Hi ployer, i´m trying to do the same as you with the same book, i cannot get my C# to work. This is my code

    Code (csharp):
    1. public class TileGenerator : MonoBehaviour
    2. {
    3.     public int numberOfTiles = 16;
    4.     public GameObject[] tileObjects;
    5.     GameObject matchOne;
    6.     string tileName1;
    7.     string[] tName1;
    8.     GameObject matchTwo;
    9.     string tileName2;
    10.     string[] tName2;
    11.     bool canClick = true;
    12.     public Vector3[] tileLocations = new Vector3[]
    13.     {
    14.         new Vector3 (0.0f, 0.0f, 0.0f), new Vector3 (1.5f, 0.0f, 0.0f),
    15.         new Vector3 (3.0f, 0.0f, 0.0f), new Vector3 (4.5f, 0.0f, 0.0f),
    16.         new Vector3 (0.0f, 1.5f, 0.0f), new Vector3 (1.5f, 1.5f, 0.0f),
    17.         new Vector3 (3.0f, 1.5f, 0.0f), new Vector3 (4.5f, 1.5f, 0.0f),
    18.         new Vector3 (0.0f, 3.0f, 0.0f), new Vector3 (1.5f, 3.0f, 0.0f),
    19.         new Vector3 (3.0f, 3.0f, 0.0f), new Vector3 (4.5f, 3.0f, 0.0f),
    20.         new Vector3 (0.0f, 4.5f, 0.0f), new Vector3 (1.5f, 4.5f, 0.0f),
    21.         new Vector3 (3.0f, 4.5f, 0.0f), new Vector3 (4.5f, 4.5f, 0.0f),
    22.     };
    23.  
    24.    
    25.     // Use this for initialization
    26.     void Start ()
    27.     {
    28.         Camera.main.transform.position = new Vector3 (2.25f, 2.25f, -8);
    29.         for (int i = 0; i<numberOfTiles; i++) {
    30.             Instantiate (tileObjects [i], tileLocations [i], Quaternion.identity)
    31.         }
    32.     }
    33.    
    34.     void Update ()
    35.     {  
    36.         if (canClick == true) {
    37.             if (Input.GetButtonDown ("Fire1")) {
    38.                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    39.                 if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
    40.                     if (!matchOne) {
    41.                         revealCardOne ();
    42.                     } else {
    43.                         revealCardTwo ();
    44.                     }
    45.                 }
    46.             }
    47.         }
    48.     }
    49.    
    50.     void revealCardOne ()
    51.     {
    52.         matchOne = hit.transform.gameObject;
    53.         tileName1 = matchOne.transform.parent.name;
    54.         if (matchOne == null) {
    55.             print ("No object found!");
    56.         } else {
    57.             tName1 = tileName1.Split ("_" [0]);
    58.             matchOne.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
    59.         }
    60.     }
    61.  
    62.     IEnumerator revealCardTwo ()
    63.     {
    64.        
    65.         matchTwo = hit.transform.gameObject;
    66.         tileName2 = matchTwo.transform.parent.name;
    67.         if (tileName1 != tileName2) {  
    68.             if (matchTwo == null) {
    69.                 print ("No object found!");
    70.             } else {
    71.                 tName2 = tileName2.Split ("_" [0]);
    72.                 matchTwo.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
    73.             }
    74.             if (tName1 [0] == tName2 [0]) {
    75.                 canClick = false;
    76.                 Destroy (matchOne);
    77.                 Destroy (matchTwo);
    78.                 canClick = true;
    79.                 numberOfTiles = numberOfTiles - 2;
    80.                 if (numberOfTiles == 0) {
    81.                 }
    82.             } else {
    83.                 canClick = false;
    84.                 matchOne.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
    85.                 matchTwo.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
    86.                 canClick = true;
    87.             }
    88.            
    89.             matchOne = null;
    90.             matchTwo = null;   
    91.            
    92.             while (tileName1 != tileName2) {
    93.             WaitForSeconds (5.0f);
    94.             }
    95.  
    96.         }
    97.     }
    98. }
    99.  
    it seems ok, but i cant get the yield to work correctly, i would appreciate your help, THANKS :):)
     
  8. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    WaitForSeconds does nothing by itself. You need the yield return new like the original poster.
     
  9. masterofyngwie

    masterofyngwie

    Joined:
    May 14, 2012
    Posts:
    9
    hi, i corrected my code, it´s a match game, i need that when two of the pieces do not match to wait a certain amount of time for the player to wait and see the other piece that doesnt match, but i cant get it to work :( i´m a begginer, i would really appreciate some help, thanks. Sorry for my english, it´s not my first language :)

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TileGenerator : MonoBehaviour
    5. {
    6.     public int numberOfTiles = 16;
    7.     public GameObject[] tileObjects;
    8.     GameObject matchOne;
    9.     string tileName1;
    10.     string[] tName1;
    11.     GameObject matchTwo;
    12.     string tileName2;
    13.     string[] tName2;
    14.     bool canClick = true;
    15.     public Vector3[] tileLocations = new Vector3[]
    16.     {
    17.         new Vector3 (0.0f, 0.0f, 0.0f), new Vector3 (1.5f, 0.0f, 0.0f),
    18.         new Vector3 (3.0f, 0.0f, 0.0f), new Vector3 (4.5f, 0.0f, 0.0f),
    19.         new Vector3 (0.0f, 1.5f, 0.0f), new Vector3 (1.5f, 1.5f, 0.0f),
    20.         new Vector3 (3.0f, 1.5f, 0.0f), new Vector3 (4.5f, 1.5f, 0.0f),
    21.         new Vector3 (0.0f, 3.0f, 0.0f), new Vector3 (1.5f, 3.0f, 0.0f),
    22.         new Vector3 (3.0f, 3.0f, 0.0f), new Vector3 (4.5f, 3.0f, 0.0f),
    23.         new Vector3 (0.0f, 4.5f, 0.0f), new Vector3 (1.5f, 4.5f, 0.0f),
    24.         new Vector3 (3.0f, 4.5f, 0.0f), new Vector3 (4.5f, 4.5f, 0.0f),
    25.     };
    26.  
    27.    
    28.     // Use this for initialization
    29.     void Start ()
    30.     {
    31.         Camera.main.transform.position = new Vector3 (2.25f, 2.25f, -8);
    32.         for (int i = 0; i<numberOfTiles; i++) {
    33.             Instantiate (tileObjects [i], tileLocations [i], Quaternion.identity)
    34.         }
    35.     }
    36.    
    37.     void Update ()
    38.     {  
    39.         if (canClick == true) {
    40.             if (Input.GetButtonDown ("Fire1")) {
    41.                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    42.                 if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
    43.                     if (!matchOne) {
    44.                         revealCardOne ();
    45.                     } else {
    46.                         revealCardTwo ();
    47.                         MatchCheck ();
    48.                     }
    49.                 }
    50.             }
    51.         }
    52.     }
    53.    
    54.     void revealCardOne ()
    55.     {
    56.         matchOne = hit.transform.gameObject;
    57.         tileName1 = matchOne.transform.parent.name;
    58.         if (matchOne == null) {
    59.             print ("No object found!");
    60.         } else {
    61.             tName1 = tileName1.Split ("_" [0]);
    62.             matchOne.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
    63.         }
    64.     }
    65.  
    66.     void revealCardTwo ()
    67.     {
    68.        
    69.         matchTwo = hit.transform.gameObject;
    70.         tileName2 = matchTwo.transform.parent.name;
    71.         if (tileName1 != tileName2) {  
    72.             if (matchTwo == null) {
    73.                 print ("No object found!");
    74.             } else {
    75.                 tName2 = tileName2.Split ("_" [0]);
    76.                 matchTwo.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
    77.             }
    78.         }
    79.        
    80.     }
    81.            
    82.     void MatchCheck ()
    83.     {
    84.         if (tName1 [0] == tName2 [0]) {
    85.             canClick = false;
    86.             print("Starting " + Time.time);
    87.             StartCoroutine(initPos(2.0F));
    88.             print("Before WaitAndPrint Finishes " + Time.time);
    89.             Destroy (matchOne);
    90.             Destroy (matchTwo);
    91.             canClick = true;
    92.             numberOfTiles = numberOfTiles - 2;
    93.             if (numberOfTiles == 0) {
    94.                 print ("End game");
    95.             }
    96.         } else {
    97.             canClick = false;
    98.             print("Starting " + Time.time);
    99.             StartCoroutine(initPos(2.0F));
    100.             print("Before WaitAndPrint Finishes " + Time.time);
    101.             matchOne.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
    102.             matchTwo.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
    103.             canClick = true;
    104.         }
    105.            
    106.         matchOne = null;
    107.         matchTwo = null;   
    108.            
    109.         if (tileName1 != tileName2) {
    110.         StartCoroutine(initPos(2.0F));
    111.  
    112.         }
    113.     }
    114.  
    115.     IEnumerator initPos (float waitTime)
    116.     {
    117.         yield return new WaitForSeconds(waitTime);
    118.         print ("WaitAndPrint " + Time.time);
    119.     }
    120. }
    121.  
     
    Last edited: May 15, 2012
  10. masterofyngwie

    masterofyngwie

    Joined:
    May 14, 2012
    Posts:
    9