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 LaurensiusTony · Jul 06, 2014 at 03:47 PM · coroutinescriptingbasics

understanding c# yield coroutines

hi, i read many article talking about coroutines using ienumerator and know that its better than update for function that i don't need to check it every frame or like function that only happen once in scene... but i still don't get it how to implement it...

so i have this script that only happen once in the beginning and i put it in update of my game maybe you can translate it into coroutines so i get more understanding about all this thing

 timer0 -= Time.deltaTime;
 if (timer0 < 0)
 {
    TweenScale.Begin(black2, .3f, new Vector3(1, 1, 0));
    if (black2.transform.localScale.x < 200)
    {
        TweenAlpha.Begin(black2, .1f, 0);
    }
    timer1 -= Time.deltaTime;    
 }
 
 if(timer1 < 0)
    StartGame();

so how is it in coroutines?

edit: and i also want to change startgame() into coroutines (its only doing 3..2...1.. ready.... start word that scaling up for sequence)

     void StartGame()
     {
         if (!isGameStart)
         {
             TweenScale.Begin(start3, .4f, new Vector3(100, 100, 1));
             if (start3.transform.localScale.x > 90)
             {
                 TweenAlpha.Begin(start3, .1f, 0);
                 TweenScale.Begin(start2, .3f, new Vector3(100, 100, 1));
             }
             if (start2.transform.localScale.x > 80)
             {
                 TweenAlpha.Begin(start2, .1f, 0);
                 TweenScale.Begin(start1, .3f, new Vector3(120, 120, 1));
             }
             if (start1.transform.localScale.x > 110)
             {
                 TweenAlpha.Begin(start1, .1f, 0);
                 TweenScale.Begin(start0, .4f, new Vector3(180, 180, 1));
             }
             if (start0.transform.localScale.x > 160)
             {
                 TweenAlpha.Begin(start0, .1f, 0);
                 isGameStart = true;
                 TweenPosition.Begin(EMPButton, .3f, new Vector3(-100, 104, 1));
             }
             //dualJoysticks.SetActive(true);
             //timer1 = 1;
         }
     }

and this is after i change into coroutines

  IEnumerator readyingGame()
     {
         while (!isGameStart)
         {
             TweenScale.Begin(start3, .4f, new Vector3(100, 100, 1));
             if (start3.transform.localScale.x > 90)
             {
                 TweenAlpha.Begin(start3, .1f, 0);
                 TweenScale.Begin(start2, .3f, new Vector3(100, 100, 1));
             }
             yield return null;
             if (start2.transform.localScale.x > 80)
             {
                 TweenAlpha.Begin(start2, .1f, 0);
                 TweenScale.Begin(start1, .3f, new Vector3(120, 120, 1));
             }
             yield return null;
             if (start1.transform.localScale.x > 110)
             {
                 TweenAlpha.Begin(start1, .1f, 0);
                 TweenScale.Begin(start0, .4f, new Vector3(180, 180, 1));
             }
             yield return null;
             if (start0.transform.localScale.x > 160)
             {
                 TweenAlpha.Begin(start0, .1f, 0);
                 TweenPosition.Begin(EMPButton, .3f, new Vector3(-100, 104, 1));
             }
             yield return null;
             isGameStart = true;
         }
     }

Comment
Add comment · Show 1
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 christoph_r · Jul 06, 2014 at 06:37 PM 0
Share

How about this? And ins$$anonymous$$d of checking that in every frame, you should probably call your code in the Start method.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jul 06, 2014 at 06:50 PM

Here is a fairly literal translation:

 IEnumerator DoIt() {
     while (timer1 >= 0) 
     {
         timer0 -= Time.deltaTime;
         if (timer0 < 0)
         {
             TweenScale.Begin(black2, .3f, new Vector3(1, 1, 0));
             if (black2.transform.localScale.x < 200)
             {
                 TweenAlpha.Begin(black2, .1f, 0);
             }
             timer1 -= Time.deltaTime;    
         }
         yield return null;
     }
 
     StartGame();
 }


Here is a bit better translation. Without know how TweenAlpha and TweenScale work, I cannot take it further.

 IEnumerator DoIt() {
     return new WaitForSeconds(timer0);

     while (timer1 >= 0)
     {
         TweenScale.Begin(black2, .3f, new Vector3(1, 1, 0));
         if (black2.transform.localScale.x < 200)
         {
             TweenAlpha.Begin(black2, .1f, 0);
         }
         timer1 -= Time.deltaTime;    
         yield return null;
     }
     
     StartGame();
 }
Comment
Add comment · Show 1 · 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 LaurensiusTony · Jul 07, 2014 at 12:01 AM 0
Share

wow thanks and i now trying to change startgame() into coroutines (see my edit) to but it stuck in TweenScale.Begin(start3, .4f, new Vector3(100, 100, 1));

avatar image
0

Answer by Juice-Tin · Jul 06, 2014 at 06:39 PM

It's basically like a function you can create that instead of just running through immediately, you can make the function wait for lengths of time.

This explains it pretty well. http://docs.unity3d.com/Manual/Coroutines.html

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Playing an Animation that is within a GameObject 1 Answer

Coroutine For Loop Will Not End 1 Answer

How do I call a function in another gameObject's script? 5 Answers

Player kill enemy by stepping on it. 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