Coroutine dosen't work

I have the following code :

Void IEnumerator Example ()
{
   A.gameObject.setActive (true);
   Yield return new WaitForSeconds (0.2f);
   A.gameObject.setActive (false);
}

I want to enable A gamrobject just for .2 seconds using the above coroutine. But this code does nothing. I am sure that the function it gets called because i have checcked with debug.log function

I’m not sure of your issue, but the code above will not compile.

  • ‘Void’ in front of IEnumerateor will not compile
  • ‘Yield’ should be lower case ‘yield’
  • ‘setActive’ should be ‘SetActive’ with upper case ‘S’.
  • It is likely that ‘A.GameObject’ should just be ‘A’;

Here is a body of working code:

using UnityEngine;
using System.Collections;

public class Bug25b : MonoBehaviour {

	public GameObject A;

	void Update() {
		if (Input.GetKeyDown (KeyCode.Space)) {
			StartCoroutine(Example());
		}
	}
	
	IEnumerator Example()
	{
		A.SetActive (true);
		yield return new WaitForSeconds (0.2f);
		A.SetActive (false);
	}
}