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 Carajote · Jan 25, 2020 at 10:10 PM · triggersfor loop

How can I break out of a for loop after a trigger event

I'm trying to create a cross pattern explosion with this code, the point is that if the fire hits a wall then I want to break out of the for loop to stop more objects being created in that axis, the next loop should still execute normally, my problem is that it doesn't I know the trigger even is called because the object on top of the wall gets deleted, but the variable stat is not changed and the for loop resumes and every element is created.

 public void explode(Vector3 fpos)
     {
         stat = true;
 
         //x axis +
         for (int i = 0; i < xplodrange + 1; i++)
         {
             if (stat)
             {
                 rpos = new Vector3(fpos.x + i, 0.75f, fpos.z);
                 Instantiate(this, rpos, Quaternion.identity);
             }
             else
             {
                 break;
             }
         }
         
         stat = true;
         
         //x axis -
         for (int i = 0; i < xplodrange + 1; i++)
         {
             if (stat)
             {
                 rpos = new Vector3(fpos.x - i, 0.75f, fpos.z);
                 Instantiate(this, rpos, Quaternion.identity);
             }
             else
             {
                 break;
             }
         }
 
         stat = true;
         
         //z axis +
         for (int i = 0; i < xplodrange + 1; i++)
         {
             if (stat)
             {
                 rpos = new Vector3(fpos.x, 0.75f, fpos.z + i);
                 Instantiate(this, rpos, Quaternion.identity);
             }
             else
             {
                 break;
             }
         }
         
         stat = true;
         
         //z axis -
         for (int i = 0; i < xplodrange + 1; i++)
         {
             if (stat)
             {
                 rpos = new Vector3(fpos.x, 0.75f, fpos.z - i);
                 Instantiate(this, rpos, Quaternion.identity);
             }
             else
             {
                 break;
             }
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("wall"))
         {
             stat = false;
             Destroy(gameObject);
         }
     }
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 Maniacbob · Jan 25, 2020 at 11:59 PM

Based on what I'm seeing here, your first function is all going to happen in one update meaning that it is unlikely verging on impossible for your stat variable to trip in the middle and cut off your instantiation calls. Unless this is a function being called repeatedly and regularly in which case this is never going to work because the first thing that the function does is set the variable back to true.

From my understanding what you want to do is spawn objects regularly until your trigger turns it off. What I would do is convert your spawning function into a coroutine with a while loop that while the trigger is on and the count is under a certain number keeps spawning objects. Store the co-routine in a variable so that you can start it, and then when your trigger is tripped it calls a stop co-routine on the object and then destroys it.

See the links for starting and stopping coroutines.

Start Coroutine Stop 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

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

121 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

Related Questions

How to enable passing through collider if speed is high enough at collision point 0 Answers

On Trigger Enter just triggers once for same object 1 Answer

Problem: One Trigger activating a seperate Trigger it is not in contact with. 1 Answer

Poblem in My Code 1 Answer

I want to make buttons by clicking one button 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