Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sausagesauce · Aug 06, 2013 at 10:15 PM · variabletimeyield waitforseconds

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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));
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sausagesauce · Aug 06, 2013 at 10:40 PM 0
Share

This does look better, but the variable still doesn't change.

avatar image Seth-Bergman · Aug 06, 2013 at 10:57 PM 0
Share

this code is fine, make sure it's saved, and that you're looking at the right thing. No reason this shouldn't work.

avatar image Sausagesauce · Aug 06, 2013 at 11:09 PM 0
Share

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?

avatar image WickeDHypnO · Aug 10, 2015 at 09:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges