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 ethensanchez7312 · Jun 16, 2020 at 04:50 PM · waitforsecondsproblem during runtime

My code is getting stuck in Waitforseconds even though am not destorying the object or setting it inactive?,Am Stuck in WaitforSeconds

My Goal: is to swap the highlghtedcolor with the selectedTargethighlight and then wait for 1 second before the selected target loses 1 HP and pass the turn over to the player.

My problem is that when the code is running and i meet the condition for the coroutine to start, it doesnt get pass the waitforseconds.

The object is not been destory or been set inactive in any way shape or form.

This is my code:

 // Update is called once per frame
 void Update()
 {
     if (GetComponent<BattleSystem>().Playerturn == false)
     {
         /*print("Enemy turn");
         GetComponent<BattleSystem>().highLightedColor.transform.position = GetComponent<BattleSystem>().BattleObjects[GetComponent<BattleSystem>().playercheck].transform.position;
         GetComponent<TargetSelection>().selectedTargetHighlight.transform.position = GetComponent<TargetSelection>().heros[num1].transform.position;*/
         StartCoroutine(holdforhighlightswap());
         /*GetComponent<TargetSelection>().heros[num1].GetComponent<PlayerStates>().hp -= 1;
          GetComponent<BattleSystem>().Playerturn = true;
          GetComponent<TargetSelection>().enabled = true;*/

     }
 }

 IEnumerator holdforhighlightswap()
 {
     print("Enemy turn");
     GetComponent<BattleSystem>().highLightedColor.transform.position = GetComponent<BattleSystem>().BattleObjects[GetComponent<BattleSystem>().playercheck].transform.position;
     GetComponent<TargetSelection>().selectedTargetHighlight.transform.position = GetComponent<TargetSelection>().heros[num1].transform.position;
     yield return new WaitForSeconds(1.0f);
     print("I waited 1 second");
     GetComponent<TargetSelection>().heros[num1].GetComponent<PlayerStates>().hp -= 1;
     GetComponent<BattleSystem>().Playerturn = true;
     GetComponent<TargetSelection>().enabled = 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 Priyanka-Rajwanshi · Jun 28, 2020 at 11:00 AM 0
Share

your coroutine is getting called multiple times in the Update function as Playerturn remains false till 1 second according to your code.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nevermiss · Jun 16, 2020 at 07:38 PM

Hi, the coroutine seems just fine.

  1. Does the "I waited 1 second" get printed?

  2. Add another print just before 'yield return new ..' and make sure everything works so far.

  3. Check whether the coroutine gets called everyframe or just once (it's in update thus it will start a new coroutine each frame while GetComponent().Playerturn == false).

My guess is that some of the GetComponent's are not working, thus the coroutine encounters an error there and stops.


Another thing you must start using is caching, so the game is much more performant:

 private BattleSystem bs;
 private TargetSelection ts;
 
 void Start()
 {
      bs = GetComponent<BattleSystem>();
      ts = GetComponent<TargetSelection>();
 }
 
 void Update()
 {
      if (bs.Playerturn == false)
      {
          StartCoroutine(holdforhighlightswap());
      }
 }
 
 IEnumerator holdforhighlightswap()
 {
      print("Enemy turn");
      bs.highLightedColor.transform.position = bs.BattleObjects[bs.playercheck].transform.position;
      ts.selectedTargetHighlight.transform.position = ts.heros[num1].transform.position;
      yield return new WaitForSeconds(1.0f);
      print("I waited 1 second");
      ts.heros[num1].GetComponent<PlayerStates>().hp -= 1;
      bs.Playerturn = true;
      ts.enabled = true;  
 }


Not only the code looks cleaner, it also runs 5x faster each time you start the coroutine.

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
avatar image
0

Answer by RodrigoAbreu · Jun 28, 2020 at 11:29 AM

Hey @ethensanchez7312,

Make sure to retrieve all of your needed components in the Start instead of using GetComponent each frame and also inside your Coroutine, as @Nevermiss has described.

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

132 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 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 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 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 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 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 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 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 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

yield in C# doesn't work, not event the sample code? 1 Answer

Wait For Seconds to Load level C# 2 Answers

is there any function call will be call after i activate an object ? 1 Answer

Invoke/yield in non MonoBehaviour classes 1 Answer

Why does Yield WaitForSeconds () only run once 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