- Home /
Wait for seconds not working second time
In my code I call Wait for Seconds multiple times. So the first time i go to updateGUIforState() in START_PRE_START case it works perfectly, but the second time when I call it from nextTutorial() in case STATE_SWIPE_STRAIGHT, it just displays the "before wait" debug log and stops, not showing the "after wait" debug log. Can someone please tell me why my WaitForSeconds function is not working. Also my gameobject is not destroyed or disabled by anyother gameobject. Please help urgent!
Here is my code :
using UnityEngine;
using System.Collections;
enum TUTORIALSTATE {
STATE_PRE_START,
STATE_SWIPE_LEFT,
STATE_SWIPE_STRAIGHT,
STATE_SWIPE_RIGHT,
STATE_COMPLETE
};
public class TutorialManager : MonoBehaviour {
static TutorialManager myInstance;
static int instances = 0;
public GameObject ftueGui;
public bool stateChanged = false;
public GameObject currentArrow = null;
Vector3 currentPosition;
bool animateArrow = false;
public GameObject dialogueBox,BlackTint;
Vector3 originalDialogueSize;
string[] Texts = new string[]{"Howdy! user,/n Lets teach you to play Flick Cricket!",
"Swipe the ball to hit it!",
"Swipe when ball is about to hit the ground",
"Now hit 3 perfect shots",
"Hit the Targets to get runs",
"Hit 2 Targets now"};
TUTORIALSTATE tutorialState = TUTORIALSTATE.STATE_COMPLETE;
//Returns the instance
public static TutorialManager Instance
{
get
{
if (myInstance == null)
myInstance = FindObjectOfType(typeof(TutorialManager)) as TutorialManager;
return myInstance;
}
}
//This function is called at the start of the game
void Start()
{
//Calibrates the myInstance static variable
instances++;
if (instances > 1)
Debug.Log("Warning: There are more than one Level Generator at the level");
else
myInstance = this;
currentArrow = null;
animateArrow = false;
originalDialogueSize = dialogueBox.transform.localScale;
}
public void initialize() {
tutorialState = TUTORIALSTATE.STATE_PRE_START;
ftueGui.transform.Find("message").gameObject.SetActive(true);
stateChanged = true;
StartCoroutine(updateGUIforState());
}
void nextTutorial() {
this.tutorialState++;
stateChanged = true;
Debug.Log ("waitingintut");
StartCoroutine (Wait ());
Debug.Log ("waitingintut1");
if (this.tutorialState == TUTORIALSTATE.STATE_COMPLETE) {
MainController.Instance.setTutorialComplete();
}
else
{
//StopCoroutine(updateGUIforState ());
StartCoroutine(updateGUIforState ());
}
}
public void nextTargetTutorial() {
if (tutorialState == TUTORIALSTATE.STATE_SWIPE_LEFT || tutorialState == TUTORIALSTATE.STATE_SWIPE_RIGHT) {
this.nextTutorial();
}
}
public void nextBallTutorial() {
if (tutorialState != TUTORIALSTATE.STATE_SWIPE_STRAIGHT) return;
this.nextTutorial();
}
//This function is called every frame
void Update()
{
if(animateArrow)
StartCoroutine(showAndAnimateArrow());
if (tutorialState == TUTORIALSTATE.STATE_COMPLETE || !stateChanged) return;
if(!stateChanged)
{
// Debug.Log ("In Pre Start");
StartCoroutine(updateGUIforState());
}
}
IEnumerator showTargetMiss() {
ftueGui.transform.Find("miss").gameObject.SetActive(true);
ftueGui.transform.Find("miss").GetComponent<TextMesh>().text = "Miss!";
yield return new WaitForSeconds(1);
ftueGui.transform.Find("miss").gameObject.SetActive(false);
GameController.Instance.stopGeneratingBalls();
yield return new WaitForSeconds(0.5f);
StartCoroutine(showArrow());
}
public IEnumerator showAndAnimateArrow() {
Debug.Log ("animate");
if (currentArrow == null) yield return new WaitForSeconds(0);
currentArrow.renderer.material.mainTextureOffset = new Vector2(0, currentArrow.renderer.material.GetTextureOffset("_MainTex").y - 0.02f);
currentArrow.transform.position = Vector3.MoveTowards(currentArrow.transform.position, currentArrow.transform.position + currentArrow.transform.forward,0.01f);
//currentArrow.transform.position = Vector3.Lerp(currentArrow.transform.position, currentArrow.transform.position + currentArrow.transform.forward,Time.deltaTime/1.5f);
Color colorStart = currentArrow.renderer.material.color;
//colorStart.a = Mathf.Lerp (colorStart.a, 0, Time.deltaTime / 0.15f);
//currentArrow.renderer.material.color = new Color(colorStart.r, colorStart.g, colorStart.b, colorStart.a);
currentArrow.renderer.material.color = new Color(colorStart.r, colorStart.g, colorStart.b, colorStart.a - 0.05f);
yield return new WaitForSeconds(0);
}
public void showMiss() {
StartCoroutine(showTargetMiss());
}
IEnumerator updateGUIforState() {
Debug.Log ("update");
switch (this.tutorialState) {
case TUTORIALSTATE.STATE_PRE_START:
ftueGui.transform.Find("message").GetComponent<TextMesh>().text = "Howdy! user,/n Lets teach you to play Flick Cricket!";
Debug.Log ("BEFORE 5");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
yield return new WaitForSeconds(1);
Debug.Log ("waiting");
StartCoroutine(AnimateDialogueBox());
Debug.Log ("AFTER 5");
//this.nextTutorial();
break;
case TUTORIALSTATE.STATE_SWIPE_STRAIGHT:
Debug.Log ("BEFORE 4");
Debug.Log ("AFTER 4");
ftueGui.transform.Find("message").GetComponent<TextMesh>().text = "Swipe to hit!";
currentArrow = ftueGui.transform.Find("state1").transform.Find("image").gameObject;
currentPosition = currentArrow.transform.position;
Debug.Log ("before wait");
yield return new WaitForSeconds(1);
Debug.Log ("after wait");
StartCoroutine(TapToContinue());
StartCoroutine(showArrow());
break;
case TUTORIALSTATE.STATE_SWIPE_LEFT:
Debug.Log("Swipe_Left");
ftueGui.transform.Find("state1").gameObject.SetActive(false);
yield return new WaitForSeconds(2);
ftueGui.transform.Find("done").gameObject.SetActive(true);
currentArrow = ftueGui.transform.Find("state2").transform.Find("image").gameObject;
currentPosition = currentArrow.transform.position;
stateChanged = false;
GameController.Instance.stopGeneratingBalls();
yield return new WaitForSeconds(1);
StartCoroutine(showArrow());
//GameController.Instance.startGeneratingBalls();
ftueGui.transform.Find("done").gameObject.SetActive(false);
ftueGui.transform.Find("message").GetComponent<TextMesh>().text = "Hit the Sign!";
break;
case TUTORIALSTATE.STATE_SWIPE_RIGHT:
ftueGui.transform.Find("state2").gameObject.SetActive(false);
ftueGui.transform.Find("done").gameObject.SetActive(true);
currentArrow = ftueGui.transform.Find("state3").transform.Find("image").gameObject;
currentPosition = currentArrow.transform.position;
stateChanged = false;
GameController.Instance.stopGeneratingBalls();
yield return new WaitForSeconds(1);
StartCoroutine(showArrow());
//GameController.Instance.startGeneratingBalls();
ftueGui.transform.Find("done").gameObject.SetActive(false);
ftueGui.transform.Find("message").GetComponent<TextMesh>().text = "Hit the Sign!";
break;
case TUTORIALSTATE.STATE_COMPLETE:
ftueGui.transform.Find("state3").gameObject.SetActive(false);
ftueGui.transform.Find("done").gameObject.SetActive(true);
stateChanged = false;
yield return new WaitForSeconds(1);
ftueGui.transform.Find("done").gameObject.SetActive(false);
ftueGui.transform.Find("message").GetComponent<TextMesh>().text = "Ready to Go!!";
animateArrow = false;
currentArrow = null;
yield return new WaitForSeconds(1);
break;
}
}
public IEnumerator Wait()
{
Debug.Log ("1");
yield return new WaitForSeconds(1);
Debug.Log ("1");
yield return new WaitForSeconds(1);
Debug.Log ("2");
yield return new WaitForSeconds(1);
Debug.Log ("3");
yield return new WaitForSeconds(1);
Debug.Log ("4");
yield return new WaitForSeconds(1);
Debug.Log ("5");
yield return new WaitForSeconds(1);
Debug.Log ("6");
yield return new WaitForSeconds(1);
Debug.Log ("7");
}
public IEnumerator showArrow() {
Debug.Log ("ARRPW");
if (currentArrow != null) {
animateArrow = true;
GameController.Instance.stopGeneratingBalls();
currentArrow.transform.position = currentPosition;
Color colorStart = currentArrow.renderer.material.color;
currentArrow.renderer.material.color = new Color(colorStart.r, colorStart.g, colorStart.b, 1.0f);
GameObject currentGameObject = ftueGui.transform.Find("state1").gameObject;
if (tutorialState == TUTORIALSTATE.STATE_SWIPE_LEFT)
currentGameObject = ftueGui.transform.Find("state2").gameObject;
if (tutorialState == TUTORIALSTATE.STATE_SWIPE_RIGHT)
currentGameObject = ftueGui.transform.Find("state3").gameObject;
currentGameObject.SetActive(true);
yield return new WaitForSeconds(0.5f);
currentGameObject.SetActive(false);
yield return new WaitForSeconds(1f);
GameController.Instance.startGeneratingBalls();
Debug.Log("showArrow");
}
}
IEnumerator AnimateDialogueBox()
{
Time.timeScale = 0;
dialogueBox.SetActive (true);
bool animate = true;
Vector3 originalSize = dialogueBox.transform.localScale;
Vector3 targetSize = dialogueBox.transform.localScale + new Vector3 (1.2f, 1.2f, 0f);
Vector3 shrinkSize = targetSize - new Vector3 (0.3f, 0.3f, 0);
bool grow = true, shrink = false;
Color c = dialogueBox.renderer.material.GetColor ("_Color");
c.a = 0;
dialogueBox.renderer.material.SetColor("_Color",c);
while(animate)
{
c.a += 0.15f;
Debug.Log ("IN ANIMATE");
dialogueBox.renderer.material.SetColor("_Color",c);
if(grow)
{
dialogueBox.transform.localScale = Vector3.MoveTowards (dialogueBox.transform.localScale,targetSize,0.35f);
if(Vector3.Distance(dialogueBox.transform.localScale,targetSize) < 0.1f)
{
grow = false;
shrink = true;
}
Debug.Log ("growing");
}
if(shrink)
{
dialogueBox.transform.localScale = Vector3.MoveTowards (dialogueBox.transform.localScale,shrinkSize,0.35f);
Debug.Log (Vector3.Distance(dialogueBox.transform.localScale,targetSize));
if(Vector3.Distance(dialogueBox.transform.localScale,targetSize) < 0.5f)
{
grow = false;
shrink = false;
animate = false;
}
Debug.Log ("shrinking");
}
yield return 0;
}
Debug.Log ("almost tap");
StartCoroutine(TapToContinue());
return true;
}
IEnumerator TapToContinue()
{
Debug.Log ("In Tap To Continue");
bool tapped = false;
while(!tapped)
{
Debug.Log ("tapped");
if(Input.GetMouseButtonDown(0) && !tapped)
{
Debug.Log ("In mouse");
nextTutorial();
ResetDialogueBox();
BlackTint.SetActive(false);
tapped = true;
}
yield return 0;
}
yield return 0;
}
void ResetDialogueBox()
{
dialogueBox.SetActive(false);
dialogueBox.transform.localScale = originalDialogueSize;
}
public int getTargetPosition() {
if (tutorialState == TUTORIALSTATE.STATE_SWIPE_LEFT) return 1;
if (tutorialState == TUTORIALSTATE.STATE_SWIPE_RIGHT) return 5;
return -1;
}
}
Answer by burost · Mar 03, 2015 at 12:15 PM
So it's been kind of a while since this was posted, but I was getting a similar issue and found the problem: my Time.timeScale was set to 0 during the calls that didn't work.
Damn. Such a little thing took away my entire morning - thank you! I had kinda been setting Time.timeScale to zero after exiting a level, as my interface requires to pause the game for doing so.
Your answer
Follow this Question
Related Questions
Coroutine running twice 1 Answer
Coroutine sequence not running properly 1 Answer
WaitForSeconds working but going infinitely 1 Answer
WaitForSeconds Not Working 4 Answers
WaitForSeconds not waiting 1 Answer