This is a 2D game, if that has anything to do with the problem. But basically, I have an instantiation, and when the object is clicked, I want it to be hidden, wait for 2 seconds, and then move to a different spot. I tried to use the coroutine strategy with yield return new WaitForSeconds(2f);, but it isn’t working.
//Coroutine code
IEnumerator waitCoroutine(){
Debug.Log("CoroutineExample started at " + Time.time.ToString() + "s");
yield return new WaitForSeconds(2f);
Debug.Log("CoroutineExample ended at " + Time.time.ToString() + "s");
}
It is printing times that are 2 seconds apart, but the object(wheat) is still appearing instantly. Can someone help?
//Reinstantiate code
if (WheatClicked){
Debug.Log("prewheatclicked");
GameObject.Find("Wheat").GetComponent<SpriteRenderer>().sortingOrder = 0;
StartCoroutine(waitCoroutine());
GameObject.Find("Wheat").GetComponent<SpriteRenderer>().sortingOrder = 1;
GameObject.Find("Wheat").transform.position = new Vector2(Random.Range(-8.21f, 17.31f), Random.Range(-4.09f, 15.13f));
playerItems.wheat += 1;
WheatClicked = false;
}
Thanks for any help!
You need to put the code that you want to run after the waitforseconds in the coroutine. Think of it as running a function. In this case you would want:
IEnumerator waitCoroutine(){
Debug.Log("CoroutineExample started at " + Time.time.ToString() + "s");
yield return new WaitForSeconds(2f);
GameObject.Find("Wheat").transform.position = new Vector2(Random.Range(-8.21f, 17.31f), Random.Range(-4.09f, 15.13f));
Debug.Log("CoroutineExample ended at " + Time.time.ToString() + "s");
}
the part of the code that moves and changes the sorting order of the wheat is outside of the coroutine, if you want it to move and show the wheat after 2 seconds you’re gonna have to put this:
GameObject.Find("Wheat").GetComponent<SpriteRenderer>().sortingOrder = 1;
GameObject.Find("Wheat").transform.position = new Vector2(Random.Range(-8.21f, 17.31f),
Random.Range(-4.09f, 15.13f));
playerItems.wheat += 1;
WheatClicked = false;
in this:
IEnumerator waitCoroutine() {
Debug.Log("CoroutineExample started at " + Time.time.ToString() + "s");
yield return new WaitForSeconds(2f);
Debug.Log("CoroutineExample ended at " + Time.time.ToString() + "s");
// you put the code here
}
Thanks! But now I am running into another issue. Input.GetMouseButtonDown is returning multiple clicks down even if I only click it once. Here is the code:
private void Update() {
if (Input.GetMouseButtonDown(0))
{
Debug.Log("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
You also have the coroutine already, and when I run the coroutine, the transform is running multiple times. Any help would be appreciated, and thanks!
//COROUTIINE
IEnumerator waitCoroutine(){
Debug.Log("CoroutineExample started at " + Tim
e.time.ToString() + "s");
GameObject.Find("Wheat").transform.position = new Vector2(1000, 1000);
yield return new WaitForSeconds(2f);
Debug.Log("CoroutineExample ended at " + Time.time.ToString() + "s");
Debug.Log("prewheatclicked");
GameObject.Find("Wheat").transform.position = new Vector2(Random.Range(-8.21f, 17.31f), Random.Range(-4.09f, 15.13f));
playerItems.wheat += 1;
WheatClicked = false;
}