Unity Community


Results 1 to 10 of 10

  1. Posts
    28

    yield return new WaitForSeconds C# help

    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. Posts
    325
    You kind of didn't paste the code, but I have a feeling from the error you're getting the coroutine is

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

    instead of

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


  3. Posts
    28
    Here is my code

    void MatchCheck()
    {

    if (tName1[0] == tName2[0])
    {
    canClick = false;

    yield return new WaitForSeconds(2);

    Destroy(matchOne);
    Destroy(matchTwo);

    canClick = true;

    numberOfTiles = numberOfTiles - 2;


    }

    if (numberOfTiles == 0)
    {
    print("End Game");
    }
    else
    {
    canClick = false;

    yield return new WaitForSeconds(2);

    print("Rotate Back");

    matchOne.transform.Rotate(new Vector3(0, -180f, 0));
    matchTwo.transform.Rotate(new Vector3(0, -180f, 0));

    canClick = true;
    }

    matchOne = null;
    matchTwo = null;
    }


  4. Location
    Bucuresti, Romania
    Posts
    873
    Code:  
    1. [COLOR="red"]IEnumerator [/COLOR]MatchCheck()
    2. {
    3. ...
    maybe like this?
    Programmer and junior artist
    www.gamedesign.ro (an attempt to create a repository of gamedev resources)
    My site www.gamingo.ro (romanian only)
    Facebook group for romanian Unity3D users


  5. Posts
    2,601
    Code:  
    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. Posts
    28
    thanks all that seemed to work!


  7. 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:  
    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. }

    it seems ok, but i cant get the yield to work correctly, i would appreciate your help, THANKS


  8. Location
    Missouri
    Posts
    1,529
    WaitForSeconds does nothing by itself. You need the yield return new like the original poster.
    Available for consulting:
    http://forum.unity3d.com/threads/960...Personal-Tutor
    http://www.unityprogrammer.com

    Just released The Farming Game: Software Edition, a remake of a popular board game. Check it out at the link below!
    http://thefarminggame.net/?ref=Pride

    For tutorials on how to use Unity go to my website at:
    http://www.unity-tutorials.com


  9. 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:  
    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. }
    Last edited by masterofyngwie; 05-14-2012 at 07:27 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •