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

You kind of didn’t paste the code, but I have a feeling from the error you’re getting the coroutine is

void COROUTINE()
{
    yield return new WaitForSeconds(x);
}

instead of

IEnumerator COROUTINE()
{
    yield return new WaitForSeconds(x);
}

Here is my code

[COLOR="red"]IEnumerator [/COLOR]MatchCheck()
{
...

maybe like this?

IEnumerator 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;
}

thanks all that seemed to work!

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

public class TileGenerator : MonoBehaviour
{
	public int numberOfTiles = 16;
	public GameObject[] tileObjects;
	GameObject matchOne;
	string tileName1;
	string[] tName1;
	GameObject matchTwo;
	string tileName2;
	string[] tName2;
	RaycastHit hit;
	bool canClick = true;
	public Vector3[] tileLocations = new Vector3[]
	{
		new Vector3 (0.0f, 0.0f, 0.0f), new Vector3 (1.5f, 0.0f, 0.0f),	
		new Vector3 (3.0f, 0.0f, 0.0f), new Vector3 (4.5f, 0.0f, 0.0f),
		new Vector3 (0.0f, 1.5f, 0.0f), new Vector3 (1.5f, 1.5f, 0.0f),
		new Vector3 (3.0f, 1.5f, 0.0f), new Vector3 (4.5f, 1.5f, 0.0f),
		new Vector3 (0.0f, 3.0f, 0.0f), new Vector3 (1.5f, 3.0f, 0.0f),
		new Vector3 (3.0f, 3.0f, 0.0f), new Vector3 (4.5f, 3.0f, 0.0f),
		new Vector3 (0.0f, 4.5f, 0.0f), new Vector3 (1.5f, 4.5f, 0.0f),
		new Vector3 (3.0f, 4.5f, 0.0f), new Vector3 (4.5f, 4.5f, 0.0f),
	};

	
	// Use this for initialization
	void Start ()
	{
		Camera.main.transform.position = new Vector3 (2.25f, 2.25f, -8);
		for (int i = 0; i<numberOfTiles; i++) {
			Instantiate (tileObjects [i], tileLocations [i], Quaternion.identity);	
		}
	}
	
	void Update ()
	{	
		if (canClick == true) {
			if (Input.GetButtonDown ("Fire1")) {
				Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
					if (!matchOne) {
						revealCardOne ();
					} else {
						revealCardTwo ();
					}
				}
			}
		}
	}
	
	void revealCardOne ()
	{
		matchOne = hit.transform.gameObject;
		tileName1 = matchOne.transform.parent.name;
		if (matchOne == null) { 
			print ("No object found!"); 
		} else { 
			tName1 = tileName1.Split ("_" [0]);
			matchOne.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
		} 
	}

	IEnumerator revealCardTwo ()
	{
		
		matchTwo = hit.transform.gameObject;
		tileName2 = matchTwo.transform.parent.name;
		if (tileName1 != tileName2) {  	
			if (matchTwo == null) { 
				print ("No object found!");
			} else { 
				tName2 = tileName2.Split ("_" [0]);	
				matchTwo.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
			} 
			if (tName1 [0] == tName2 [0]) {
				canClick = false;
				Destroy (matchOne);
				Destroy (matchTwo);
				canClick = true;
				numberOfTiles = numberOfTiles - 2;
				if (numberOfTiles == 0) {
				}
			} else {
				canClick = false;
				matchOne.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
				matchTwo.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
				canClick = true;
			}
			
			matchOne = null;
			matchTwo = null;	
			
			while (tileName1 != tileName2) {
			WaitForSeconds (5.0f);
			}

		}
	}
}

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

WaitForSeconds does nothing by itself. You need the yield return new like the original poster.

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 :frowning: i´m a begginer, i would really appreciate some help, thanks. Sorry for my english, it´s not my first language :slight_smile:

using UnityEngine;
using System.Collections;

public class TileGenerator : MonoBehaviour
{
	public int numberOfTiles = 16;
	public GameObject[] tileObjects;
	GameObject matchOne;
	string tileName1;
	string[] tName1;
	GameObject matchTwo;
	string tileName2;
	string[] tName2;
	RaycastHit hit;
	bool canClick = true;
	public Vector3[] tileLocations = new Vector3[]
	{
		new Vector3 (0.0f, 0.0f, 0.0f), new Vector3 (1.5f, 0.0f, 0.0f),	
		new Vector3 (3.0f, 0.0f, 0.0f), new Vector3 (4.5f, 0.0f, 0.0f),
		new Vector3 (0.0f, 1.5f, 0.0f), new Vector3 (1.5f, 1.5f, 0.0f),
		new Vector3 (3.0f, 1.5f, 0.0f), new Vector3 (4.5f, 1.5f, 0.0f),
		new Vector3 (0.0f, 3.0f, 0.0f), new Vector3 (1.5f, 3.0f, 0.0f),
		new Vector3 (3.0f, 3.0f, 0.0f), new Vector3 (4.5f, 3.0f, 0.0f),
		new Vector3 (0.0f, 4.5f, 0.0f), new Vector3 (1.5f, 4.5f, 0.0f),
		new Vector3 (3.0f, 4.5f, 0.0f), new Vector3 (4.5f, 4.5f, 0.0f),
	};

	
	// Use this for initialization
	void Start ()
	{
		Camera.main.transform.position = new Vector3 (2.25f, 2.25f, -8);
		for (int i = 0; i<numberOfTiles; i++) {
			Instantiate (tileObjects [i], tileLocations [i], Quaternion.identity);	
		}
	}
	
	void Update ()
	{	
		if (canClick == true) {
			if (Input.GetButtonDown ("Fire1")) {
				Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
					if (!matchOne) {
						revealCardOne ();
					} else {
						revealCardTwo ();
						MatchCheck ();
					}
				}
			}
		}
	}
	
	void revealCardOne ()
	{
		matchOne = hit.transform.gameObject;
		tileName1 = matchOne.transform.parent.name;
		if (matchOne == null) { 
			print ("No object found!"); 
		} else { 
			tName1 = tileName1.Split ("_" [0]);
			matchOne.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
		} 
	}

	void revealCardTwo ()
	{
		
		matchTwo = hit.transform.gameObject;
		tileName2 = matchTwo.transform.parent.name;
		if (tileName1 != tileName2) {  	
			if (matchTwo == null) { 
				print ("No object found!");
			} else { 
				tName2 = tileName2.Split ("_" [0]);	
				matchTwo.transform.eulerAngles = new Vector3 (0.0f, 180.0f, 0.0f);
			} 
		}
		
	}
			
	void MatchCheck ()
	{
		if (tName1 [0] == tName2 [0]) {
			canClick = false;
			print("Starting " + Time.time);
        	StartCoroutine(initPos(2.0F));
        	print("Before WaitAndPrint Finishes " + Time.time);
			Destroy (matchOne);
			Destroy (matchTwo);
			canClick = true;
			numberOfTiles = numberOfTiles - 2;
			if (numberOfTiles == 0) {
				print ("End game");
			}
		} else {
			canClick = false;
			print("Starting " + Time.time);
        	StartCoroutine(initPos(2.0F));
        	print("Before WaitAndPrint Finishes " + Time.time);
			matchOne.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
			matchTwo.transform.eulerAngles = new Vector3 (0.0f, 0.0f, 0.0f);
			canClick = true;
		}
			
		matchOne = null;
		matchTwo = null;	
			
		if (tileName1 != tileName2) {
		StartCoroutine(initPos(2.0F));

		}
	}

	IEnumerator initPos (float waitTime)
	{
		yield return new WaitForSeconds(waitTime);
		print ("WaitAndPrint " + Time.time);
	}
}

Here it is the project :slight_smile:

http://www.mediafire.com/?5ubq3dmcm79akjl