- Home /
yield WaitForSeconds(5); IS WAITING FOR EVER
Here is my code
function Start (){
//Some Stuff is here
yield WaitForSeconds(5);
if(Hart1.GetComponent(SpriteRenderer).sprite == hart){
All();
}
else if(Hart2.GetComponent(SpriteRenderer).sprite == hart){
Two();
}
function All (){
//some stuff
}
function Two (){
//some stuff
}
The problem is function All () never called I tried to put
print("All Called");
in function All () but still it never called nor function Two() if I put it in there
What am doing wrong? How can I fix it?
put your waitForSeconds stuff in to a IEnumerator function and call it ins$$anonymous$$d of directly say waitForSeconds, just like
function Start ()
{
//Some Stuff is here
Wait();
if(Hart1.GetComponent(SpriteRenderer).sprite == hart){
All();
}
else if(Hart2.GetComponent(SpriteRenderer).sprite == hart){
Two();
}
}
function All (){
//some stuff
}
function Two (){
//some stuff
}
IEnumerator Wait()
{
yield return new WaitForSeconds (2);
}
and yes you are missin a }:)
Answer by Mr. Mud · Feb 01, 2015 at 12:27 PM
The issue here might come from something different than the "WaitForSeconds(5)". You said that you tried putting a Debug in either function which can be called, but neither of them printed anything. These functions however only get executed when a certain condition is met: the sprite in the sprite renderer equals hart. If neither of them is equal, none of the functions will be called.
I would suggest to add some more debug logs to your code, as this can give a hint on where the issue arises. Here is a sample:
#pragma strict
var hart1:GameObject;
var hart2:GameObject;
var hart:Sprite;
function Start ()
{
//If this debug does not show, your script is most likely disabled.
Debug.Log("Before yield");
yield WaitForSeconds(5);
//Yield did not give any problems; it is five seconds later.
Debug.Log("5 seconds later.");
//NOTE: Make sure you assigned objects and they have a SpriteRenderer attached to them.
//If not, the next piece of code will result in NullPointerExceptions.
if (hart1.GetComponent(SpriteRenderer).sprite == hart)
{
Debug.Log("Hart1 equals hart");
All();
}
else if (hart2.GetComponent(SpriteRenderer).sprite == hart)
{
Debug.Log("Hart2 equals hart");
Two();
}
else
{
//Show the names of the assigned sprites. to figure out where it went wrong.
Debug.Log("hart1: " + hart1.GetComponent(SpriteRenderer).sprite);
Debug.Log("hart2: " + hart2.GetComponent(SpriteRenderer).sprite);
Debug.Log("hart: " + hart);
}
}
function All()
{
Debug.Log("Entered All");
}
function Two()
{
Debug.Log("Entered Two");
}