Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by huntermccoid · Aug 30, 2021 at 11:58 PM · randomcoroutinerandom.rangeyield waitforsecondsbooleans

Coroutine not working as expected

I'm trying to run a couple of coroutines that will ultimately do this: generate a random int and then start the attack function that corresponds to that int, then wait some amount of time before repeating the process.But for some reason the way my code is now random ints are being generated constantly so the enemy is just shooting every attack out extremely quickly. Can anyone help me find the flaw in my code?

  private int[] elements;
     private int myElement;
     public float timeBetweenAttacks;
     public bool runningCR;
 
     public Transform wavePoint, slicePoint, shootPoint;
 
     public bool runningAtk;
     public GameObject bossAtkWave, bossAtkSlice, bossAtkShoot;
 
 
     // Start is called before the first frame update
     void Start()
     {
         elements = new int[3];
         //BossBattleTrigger.instance = GetComponent<BossBattleTrigger>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (!runningCR && !runningAtk)
         {
             StartCoroutine(ChooseAttack());
         }
     }
 
 
 
     public IEnumerator ChooseAttack()
     {
         if (!runningAtk)
         {
             runningCR = true;
             yield return new WaitForSeconds(timeBetweenAttacks * Time.deltaTime);
 
             RandomNumberGenerator();
 
             Debug.Log(myElement);
             //Debug.Log(elements.Length);
 
             switch (myElement)
             {
                 case 0:
                     StartCoroutine(Wave());
                     break;
 
                 case 1:
                     StartCoroutine(Slice());
                     break;
 
                 case 2:
                     StartCoroutine(Shoot());
                     break;
             }
 
             //yield return new WaitForSeconds(timeBetweenAttacks * Time.deltaTime);
             runningAtk = true;
             runningCR = false;
         } else if (runningAtk)
         {
             yield break;
         }
     }
 
     public IEnumerator Wave()
     {
         //runningAtk = true;
         //do atk stuff
         Instantiate(bossAtkWave, wavePoint.transform.position, wavePoint.transform.rotation);
         yield return new WaitForSeconds(timeBetweenAttacks * Time.deltaTime);
         runningAtk = false;
         //Debug.Log("Wave");
     }
 
     public IEnumerator Slice()
     {
         //runningAtk = true;
         Instantiate(bossAtkSlice, slicePoint.transform.position, slicePoint.transform.rotation);
         yield return new WaitForSeconds(timeBetweenAttacks * Time.deltaTime);
         runningAtk = false;
         //Debug.Log("SliceRunning");
     }
 
     public IEnumerator Shoot()
     {
         //runningAtk = true;
         Instantiate(bossAtkShoot, shootPoint.transform.position, shootPoint.transform.rotation);
         yield return new WaitForSeconds(timeBetweenAttacks * Time.deltaTime);
         runningAtk = false;
         //Debug.Log("ShootRunning");
     }
 
     private void RandomNumberGenerator()
     {
         myElement = Random.Range(0, elements.Length);
     }
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
1
Best Answer

Answer by xxmariofer · Aug 31, 2021 at 07:05 AM

whats the value of timeBetweenAttacks? simply remove the Time.deltaTime, it makes no sense in your code, since that code, even if its running in the update, is not frame rate independant

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 huntermccoid · Aug 31, 2021 at 02:51 PM 0
Share

I removed the Time.deltaTime from the script and it worked exactly as expected! Thank you, haha and sorry for the newbie mistake

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

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

How do I generate a random number in regular time intervals? 0 Answers

Use all variables from array (in Random.Range()) 2 Answers

C# coroutine not waiting on waitForSeconds? 2 Answers

how to randomly choose a value from array 3 Answers

Trying to implement xorshift with deplorable results (PRNG) 0 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