• 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 MrHasuu · Jun 16, 2012 at 07:42 PM · delaywait

delay or waiting?

Hi, i'm trying to write up a script that plays an animation when a key is pressed on the screen, and immediately after moves the camera to another location. but i've encountered a problem where the animation will play after the key is pressed and the camera will move at the same time so before the animation even got through the first frame i no longer see it. is there a type of delay or wait method that can allow me to wait 1 second after so i can make the animation visible before moving the camera?

below is the function i've set up for it.

 public void Anime(){
         Instantiate(explosion, touchpos, Quaternion.identity); //explosion animation
         hit.transform.SendMessage("HideMe"); //disable renderer for object
         Instantiate(shatter, hit.transform.position, Quaternion.identity); //shattering animation of object
         //delay script here ??
         MoveCamera();
         hit.transform.SendMessage("ShowMe"); //re-enable render for object
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Wolfram · Jun 16, 2012 at 07:57 PM

Make it a Coroutine, and then use WaitForSeconds as described in that doc page. Note in C# you'll need to call this function using StartCoroutine(Anime()) instead of a regular function call.

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 MrHasuu · Jun 16, 2012 at 08:13 PM 0
Share

I've never used Coroutine before so im not sure if im doing it correctly, i've got this function that'd wait for 1 second

IEnumerator WaitOneSecond() { yield return new WaitForSeconds(1); }

then i call it in my function with StartCoroutine( WaitOneSecond() ); ? because when i did it this way it didnt delay at all

avatar image whydoidoit · Jun 16, 2012 at 09:06 PM 1
Share

@wolfram means that you make your whole function an IEnumerator and start Anime using StartCoroutine, then you insert yield return new WaitForSeconds(1); where you have the comment in your code.

avatar image MrHasuu · Jun 16, 2012 at 09:29 PM 0
Share

oooh okay i got it now, thank you very much. i changed the function into an IEumerator and used StartCoroutine(Anime()); to start it then i realize i needed the line of code that changes camera view to be in the function as well which was causing me the problem.

avatar image Wolfram · Jun 16, 2012 at 09:38 PM 0
Share

Ah, sorry I didn't get back to you. Thanks @whydoidoit :-) Also, I wrote "Awake" ins$$anonymous$$d of "Anime"...must have been a reflex...

The idea is, if you use StartCoroutine(...) somewhere, it launches a function which runs separately from the rest of your code, your normal code will continue immediately, directly after the line StartCoroutine(...). Within the coroutine, you can place "yield" statements, to wait for a single frame, or to wait for several seconds, and so on.

avatar image
1

Answer by MrHasuu · Jun 22, 2012 at 09:56 AM

for anyone in the future that come across this: my problem was that i assumed that i can have the below script as a function i can call up whenever i needed to have a 1 second delay in my coding.

 IEnumerator WaitOneSecond() { 
     yield return new WaitForSeconds(1); 
 }

like something like this was what i assumed it to be

 void Update() {
     //do some stuff
     StartCoroutine(WaitOneSecond()); //delay for a second
     //continue do some other stuff
 }

But this is incorrect. in the end i had something like this that worked

 IEnumerator AnimeNwait(){    
     Instantiate(explosion, touchpos, Quaternion.identity);
     
     hit.transform.SendMessage("HideMe"); //disable renderer
     Instantiate(shatter, hit.transform.position, Quaternion.identity);
     
      yield return new WaitForSeconds(0.8f);
     _MainMenuCameraShift.SettingisClicked = true;
      yield return new WaitForSeconds(0.8f);
     hit.transform.SendMessage("ShowMe");
 }

and i called this coroutine with StartCoroutine(AnimeNWait()); in the script when needed

Comment
Add comment · Show 3 · 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 Grixen · Jul 12, 2015 at 03:49 PM 0
Share

$$anonymous$$rHasuu - Hello.

You wrote: something like this was what i assumed it to be void Update() { do some StartCoroutine(WaitOneSecond()); //delay for a second continue do some other stuff

Can you PLEASE show your solution nor from your code but just like you showed as it doesnt work.

Its some kind of a conspiracy. Everybody talking about starting coroutine but nobody show where should be my void update in all of this.

avatar image Wolfram · Jul 13, 2015 at 10:32 AM 1
Share

The one has nothing to do with the other. Anything in Update() will be executed each frame, immediately. Anything in a Coroutine will be executed according to its yield commands.

So putting a StartCoroutine() into Update() would launch a new instance of that coroutine every frame, which is almost certainly not what you want. Ever.

Ins$$anonymous$$d, start your Coroutine once, wherever (for example in Start()). If you then feel the need to do something every frame in that coroutine, use a loop with a "yield return 1;" in it.

avatar image Grixen · Jul 14, 2015 at 02:04 AM 0
Share

Tnx, it make sense now.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Slowing down the speed of my bullets whilst holding in the key. 1 Answer

animation delay or wait 4 Answers

Wait until particle is done playing 1 Answer

destroy object after a delay? 6 Answers

WaitForSeconds Question 2 Answers


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