Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Corza334 · Aug 08, 2011 at 12:14 PM · c#yielddelayienumerator

Create a delay between function executions

I've been reading over the C# Yield answers for hours - I cannot seem to make anything work with what I want to do. Half my game is created and I can't believe this is where I got stuck! Haha!

So, What I want to have happen is a delay inbetween some functions...

 `public void executeAttack(int pa){
     
     /*
      * Enemy select attack based on players selected attack and weakest defence.
      * Attempt to execute an attack BASED ON THE SPEED OF THE PLAYER AND OPPONENT
      * To do that, if (PlayerSpeed > Enemy){player.Attack(); enemy.Attack()} else { enemy.Attack(); player.Attack() }
      */
         if ((player.Vitals[1] - skillist.skills[pa,5,0,0]) >= 0){ // Check if you have enough AP to attack
             
             enemy.SelectedAttack = enemySelectAttackAI(pa); // Enemy select Attack AI (Currently just random based on what skills the enemy has)
             player.SelectedAttack = pa;                        // Set the player selectedAttack
             
             if(player.Attributes[6] >= enemy.Attributes[6]){  // Attribute[6] = speed
             
                 player.Attack(enemy);
                  //I want to create delays in between these functions!
                 player.checkStatusEnd();
                 checkHealth(enemy);           // Should make this into a function
                 checkReason(player);
                 
                 enemy.Attack(player);
                 enemy.checkStatusEnd();
                 checkHealth(player);
                 checkReason(enemy);
                 
             } else if (player.Attributes[6] > enemy.Attributes[6]) {   // Need a better way to display messages...
                 enemy.Attack(player);
                  yield return new WaitForSeconds( 2.5f );
                 enemy.checkStatusEnd();
                 checkHealth(player);
                 checkReason(enemy);
                 
                 player.Attack(enemy);
                 player.checkStatusEnd();
                 checkHealth(enemy);
                 checkReason(player);
             }
         } else {
             showMessage("You do not have enough AP to use that attack");
         }
 }`

Now I tried so many different solutions... Obviously, I tried make executeAttack return IEnumerator... no joy.

I've tried creating while loops that loop through till time > x (x being the delay time) Most my results end in either 1) Nothing at all happening 2) Infinite Iterations (Unity Crashes) 3) If I call a seperate delay function using StartCoroutine(delay(10)); etc, it just runs along side the code I'm trying to pause.

Most of these are actually expected results.. I'm just baffled as to the logic behind making this work.

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
0
Best Answer

Answer by SilverTabby · Aug 08, 2011 at 03:43 PM

Your main problem is that your method has to 1) say it returns an IEnumerator, and 2) actually return one. The statement yield return returns the correct IEnumerator and will cause it to compile fine.

I'll try to provide a working example for C# coroutines. Hopefuly this will help you figure out what's going on.


 public bool useEarlyOut = false;
 public void Start()
 {
     
     //creates a coroutine that is completely independent of this method.
     StartCoroutine(timingTest());
 }

 public IEnumerator timingTest()
 {
     //creates a coroutine that is completely independent of this method.
     StartCoroutine(waitAndPrint("Third, this is printed", 1.0));
     StartCoroutine(waitAndPrint("Second, this is printed", 0.5));
     Debug.Log("First, this is printed");

     //waits until this coroutine finishes.
     yield return StartCoroutine(waitAndPrint("Fourth, this is printed", 1.5));
     Debug.Log("Fifth this is printed");

     if(useEarlyOut == true)
    {
         Debug.Log("The Loop will not be printed");
 
         //kills this coroutine.
         yield return break;
    }

     for(int count = 10; count > 0; count--)
     {
         Debug.Log("This will be printed "+count+" more times, once every frame.");
         yield return null; //waits a frame
     }
 }    

 public IEnumerator waitAndPrint(String text ,float howLong)
 {
     //waits until this amount of time has passed.
     yield return new WaitForSeconds(howLong);
     Debug.Log(text);
 }



yield return ____________________________________________;

- null //waits for one frame to pass and resumes during the Update calls

- break //kills this coroutine immediately.

- StartCoroutine(nameOfCoroutine()) //waits for nameOfCoroutine to finish

- new WaitForSeconds(X) //waits for X seconds

- new WaitForFixedUpdate() // waits for one frame to pass and resumes during the FixedUpdate calls

Comment
Add comment · Show 2 · 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 Corza334 · Aug 08, 2011 at 11:27 PM 0
Share

Hmm thanks for that, I guess a full rethinking of the program is in order.

Well - I guess this is how you learn! Will plan for this in future.

Thanks again

avatar image Corza334 · Aug 14, 2011 at 12:19 PM 0
Share

Works like a charm! Thanks a million.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What am I doing wrong with IEnumerator? 1 Answer

Having trouble refactoring an IEnumerator method with multiple yields 1 Answer

Problem using yield and IEnumerator in a custom function 2 Answers

Delay If statement in Update c# 2 Answers

IEnumerator not working 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