- Home /
Script gets stuck on WaitForSeconds()
I have this script that creates a puddle for every 0.5 seconds. Though when the script gets to "yield return new WaitForSeconds()", it just stops right there. The rest of the FixedUpdate works but the CreatePuddle() is broken because of the WaitForSeconds(). Here's the script:
using UnityEngine;
using System.Collections;
public class SlimeScript : EnemyScripts {
//===========//
//[VARIABLES]//
//===========//
//Public Variables
public GameObject puddle; //Puddle prefab
public float delay; //Delay between puddle creations
//Private variables
bool canCreatePuddle = true; //Can the script create another puddle?
//===========//
//[FUNCTIONS]//
//===========//
//Creates a puddle
IEnumerator CreatePuddle(){
canCreatePuddle = false;
//Instantiate a puddle and sets the values
Vector3 pos = new Vector3 (transform.position.x,transform.position.y,transform.position.z);
GameObject newPuddle = Instantiate (puddle,pos,Quaternion.Euler(Vector3.zero)) as GameObject;
newPuddle.transform.parent = transform.parent;
newPuddle.GetComponent<DamageDealer> ().damage = damage;
newPuddle.GetComponent<DamageDealer> ().friendly = false;
//Delay
print ("Created puddle");
yield return new WaitForSeconds (0f);
print ("Create new puddle");
//Allow the script create another puddle
canCreatePuddle = true;
}
public override void FixedUpdate(){
base.FixedUpdate (); //Does everything that's in the base's FixedUpdate()
//Create's a puddle
if (canCreatePuddle)
StartCoroutine (CreatePuddle());
print ("Done");
}
}
UPDATE: https://youtu.be/uJURvk7rWB4. I'm still with the issue. I removed the enemy from the level and it worked.
Try putting in "0.0001f" ins$$anonymous$$d of 0f. That might be the issue.
If it prints, "Created Puddle", then the co routine is being called. If it prints, "Create new puddle", then it gets past the co routine. Otherwise, it's that causing it.
yield return new WaitForSeconds (0f);
Did you try WaitForSeconds with a value > 0? $$anonymous$$aybe it's bugged and waits forever if you pass 0.
Yes, i did. I also forgot to mention that it didn't print "Created new puddle".
Did you try starting the Coroutine from Update()? $$anonymous$$aybe the FixedUpdate() is the problem.
Ok so i tested something and it worked, though i had to remove the enemy out of the level. I will be making a video showing it.
Answer by Dragon-Of-War · Sep 03, 2016 at 12:43 AM
The problem wasn't with the script. I guess the rooms switched deactivated in a way the the script glitched.
Your answer
Follow this Question
Related Questions
Another yield not working problem 2 Answers
Is there any way to stop WaitForSeconds() ? 1 Answer
Understanding yield inside a for or a while loop 2 Answers
Waypoint / Yield help 1 Answer
lock a target after a few seconds 1 Answer