- Home /
Yield Waitforseconds not working at all
Yield just WON'T WORK. I've tried everything, and I don't understand it. Unity just refuses to wait. What I'm trying to do is I want an object to move upwards after 8 seconds have passed. Here's my javascript code:
//Whether it's waited or not
var dontchangethis : boolean;
//Set the variable to false
dontchangethis=false;
//Wait 8 Seconds
yield WaitForSeconds (8);
//Set the
dontchangethis=true;
function Update(){
if (dontchangethis){
transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
}
}
It won't set the variable to true, because it doesn't wait! What's wrong?
EDIT: I made some changes to the code, with coroutines in mind. Sadly, it still doesn't work: Here's my code again:
//Whether it's waited or not
var dontchangethis : boolean = false;
//Wait 8 Seconds
function Start(){
yield StartCoroutine(Wait());
}
function Update(){
if (dontchangethis == true){
transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
}
}
function Wait(){
yield WaitForSeconds(1);
dontchangethis=true;
}
ANOTHER EDIT: Okay, this is wierd. I imported the script into a new project and it worked, but in the old one it doesn't! So I guess the code works but I have to find the new problem.
Here's some tips.
Use Debug.Log() to print out stuff to help you debug. I'd do something like this:
var dontchangethis : boolean; //this is a global variable
function Start(){
//Set the variable to false
dontchangethis=false;
Debug.Log("Before yield");
yield WaitForSeconds(8);//Wait 8 Seconds
Debug.Log("After yield");
//Set the variable to true
dontchangethis=true;
}
function Update() {
if (dontchangethis){ //If the variable's true, then move upwards
transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
}
}
Then when you click play, you'll see in the console "Before yield". 8 seconds later, you'll see "After yield". Also, this code works exactly like expected, when it's on a cube, after 8 seconds the cube launches.
In response to your edit, you should do:
StartCoroutine(Wait());
Ins$$anonymous$$d of
yield StartCoroutine(Wait());
Please let us know if this issue has been resolved. If it has been, and one of these answers helped, please be so kind to accept and vote up the answer that best provides a proper solution.
Answer by clunk47 · Aug 07, 2013 at 05:54 PM
It's working fine on my end. All I needed to do to check this was send the event to Debug Log. I usually put things like this in Start or Awake, even though you don't HAVE to in JS, it just seems more efficient to me.
var dontchangethis : boolean = false;
function Start()
{
yield WaitForSeconds (3);
dontchangethis=true;
}
function Update()
{
if (dontchangethis)
{
Debug.Log(dontchangethis);
transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
}
}
Answer by creighcl · Aug 07, 2013 at 05:41 PM
It's possible you have provided an incomplete example of what you're trying to do.
In case it isn't what you've done, be sure that your YIELD statements are inside of a Coroutine.
Don't attempt to use it elsewhere, and certainly not within the class script as it appears you are doing.
You'll want to call your Coroutine and that's where you will do your YIELD.
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html
hope this helps :-)
In javascript, you can actually do it anywhere. It's dirty, nasty and makes me sad, but you can.
Answer by perchik · Aug 07, 2013 at 06:08 PM
Since the code works somewhere else, is there any chance that you are stopping time somewhere in your current project, by setting Time.timeScale = 0
?
I doubt it, because i've tried putting it into a new scene with nothing in it but the camera, and a cube with that script.
Your answer
Follow this Question
Related Questions
Static Variable Problem 1 Answer
Function with itself called inside it. (JavaScript) 2 Answers
Yield WaitForTrue? 1 Answer
Change Variable on Another Script 2 Answers