- Home /
 
Wait a specific time?
I want to have an object move upwards after 8 seconds have passed. I have a script, and it's not working. Here's the javascript:
 //Whether it's waited or not
 var dontchangethis : boolean;
 //Set the variable to false
 dontchangethis=false;
 //Wait 8 Seconds
 yield WaitForSeconds (8);
 //Set the variable to true
 dontchangethis=true;
 //If the variable's true, then move upwards
 function Update(){
     if (dontchangethis){
         transform.Translate(Vector3.up * (Time.fixedDeltaTime * 5));
     }
 }
 
               Any help?
Answer by perchik · Aug 06, 2013 at 10:20 PM
Your code is right, but you have to be in a function for anything to happen. If you want to have it wait 8 seconds at start:
 var dontchangethis : boolean; //this is a global variable
 function Start(){
     //Set the variable to false
     dontchangethis=false;
     //Wait 8 Seconds
     yield WaitForSeconds(8);
 
     //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));
     }
 }
 
              This does look better, but the variable still doesn't change.
this code is fine, make sure it's saved, and that you're looking at the right thing. No reason this shouldn't work.
I've created an empty scene, positioning a cube in front of the main camera, added the script, and after 8 seconds, nothing happens. Could it be that the yield function is delaying the update function somehow?
For anyone looking for code in C#: Easiest way to do this is to call an Invoke() function
 void Start(){
      //Set the variable to false
      dontchangethis=false;
      //Wait 8 Seconds then call function
      Invoke("ChangeThis", 8f);
  }
 void ChangeThis()
 {
      //Set the variable to true
      dontchangethis=true;
 }
                 Your answer
 
             Follow this Question
Related Questions
Delaying a dynamic Variable(transform.position for Ex.) 2 Answers
Played time? 2 Answers
Creating a variable jump using deltaTime in C#. 0 Answers
Random time interval 1 Answer
timed value increase? 1 Answer