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 ajewell503 · May 10, 2020 at 11:16 AM · collision2dienumeratorfor-loop

How to end a for loop that is inside of an IEnumerator and is being called from another script

In my game I am trying to increase a number(ammo) while the player is standing under and beam of light. So I have an OnTriggerEnter2D on my player that detects for when the player is touching the beam of light. When the player is touching the beam it starts a Coroutine in another script that starts a for loop to add ammo every second. Then in my OnTriggerEnter2D it also detects if the player isnt touching the beam and is supposed to end the Coroutine with the for loop. However the for loop will not end no matter what until it is finished. This script is on my player:

  public void OnTriggerEnter2D(Collider2D collider)
     {
         Debug.Log(collider);
         if (collider.gameObject.name == "Sunlight")
         {
                 StartCoroutine(other.BeamCount(true));
         }
         if (collider.gameObject.name != "Sunlight")
         {         
                 StartCoroutine(other.BeamCount(false));
         }
     }

This script is on the object where my bullets are spawned:

 public int beamCount;
 
  public IEnumerator BeamCount(bool check)
     {
         Debug.Log(check);
         if (check == false)
         {
             yield return beamCount;
         }
         if (check == true)
         {
             for (int i = 0; i <= 50; i++)
             {
                 beamCount = i;
                 Debug.Log(beamCount);
                 yield return new WaitForSeconds(1);
             }
         }
     }

If anyone could figure out how to end the for loop when the player is not touching the beam it would be very much appreciated. Thank you in advanced.

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

3 Replies

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

Answer by ajewell503 · May 13, 2020 at 04:55 AM

Hey everyone, thank you all for all the help but I have found the answer!

  public void OnTriggerStay2D(Collider2D collider)     
     {
         if (collider.gameObject.name == "Sunlight")
         {
             Debug.Log(beamCount);
             currentBeamTime += Time.deltaTime;
             if (currentBeamTime > beamTime && beamCount <= 4)
             {
                 beamCount += 1;
                 currentBeamTime = 0f;
             }
 
         }
     }

I learned I needed to use OnTriggerStay2D in order to not have to move the object for the ammo counter to go up, and I had definitely way over complicated it.

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 sacredgeometry · May 10, 2020 at 02:59 PM

It sounds like you have really overcomplicated this. Why not just have a while loop or add an if statement that checks if the player is under your healing ray?


Other than that there are probably better design patterns that could be employed to write this code. i.e. players conform to an interface, on entering the ray either by using a sphere cast, on trigger enter or equivalent you test to see if that object conforms to that interface and if so on a timed interval you add a set amount of points to that players health field.

This method would scale across multiple players using the same health area at once, and wouldnt take half as much code.

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 ajewell503 · May 10, 2020 at 08:26 PM 0
Share

Thank you for your help, after sleeping on it, this is definitely too complicated. I will be trying a while loop to see if that fixes this. I would try to give a shot at a different design pattern that you have suggested, but I am struggling to understand what you are trying to suggest. Sorry about that, I would like to note that I have very little experience coding and using unity. I tried to get started on a game but realized that I didn't know how to do much. So I joined a game jam that is a week long so I can develop skills. So this will hopefully be my first complete game.

avatar image
0

Answer by JonnyHilly · May 10, 2020 at 03:39 PM

yield break; //exits an ienumerator.

but you might want to re-think the logic here a little. seems a little over complex something like this should work.... Should allow player to enter and edit the light, to get charge, until either the player is full or the charger is out of charge.

 while(check && chargeLeft>50 && playerBullets<max )   // while player under light, and charge remaining, and player not full, then give to player
 {
     playerBullets ++;
     chargeLeft --;
     yield return null;
 }

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

131 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

Related Questions

For loop & Foreach don't complete inside IEnumerator when using IndexOf 2 Answers

How to make loop with call to IEnumerator actually pause? 2 Answers

Updating waitforseconds in IEnumerator 1 Answer

Call a new IEnumerator 2 Answers

Spawn Objects Inside IEnumerator In Serverside (Unity Networking) 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