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

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:
    3,752
    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:
    966
    Code (csharp):
    1.  
    2. [COLOR="red"]IEnumerator [/COLOR]MatchCheck()
    3. {
    4. ...
    5.  
    maybe like this?
     
  5. appels

    appels

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

    masterofyngwie

    Joined:
    May 14, 2012
    Posts:
    9