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 zacharif · Dec 22, 2019 at 08:08 PM · 3dscripting beginnerclonestop

How to stop all StopAllCoroutines(); from another script

Hi! I got a cloning script provided by forums and I want to stop all coroutines after 35 seconds. But when I do that in the script it does not work. Is there any way to do this by stopping all coroutines via another script? Or do I just have to move it? Please let me know, here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

  public class clonefor4 : MonoBehaviour
 {
 public GameObject MyObjectToClone;
 Vector3 offset = new Vector3(2, 0, 2);

 // Start is called before the first frame update
 void Start()
 {
     StartCoroutine("Spawner");
 }

 IEnumerator Spawner()
 {
     while (true)
     {
         GameObject clone = Instantiate(MyObjectToClone, transform.position + offset, Quaternion.identity);
         clone.transform.position = new Vector3(clone.transform.position.x, 8f, clone.transform.position.z);
         clone.transform.position = transform.position + offset;
         clone.transform.position = new Vector3(clone.transform.position.x, 4f, clone.transform.position.z);
         **yield return new WaitForSeconds(35);
         StopAllCoroutines();**

     }
 }

}

Thanks!


EDIT: Reormatted by Captain_Pineapple

Comment
Add comment · Show 5
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 cgklutts · Dec 22, 2019 at 08:38 PM 0
Share

Can you format this code so everyone can read it?

avatar image cgklutts cgklutts · Dec 22, 2019 at 08:51 PM 0
Share

Can you explain what exactly your trying to accomplish with this code?

avatar image zacharif cgklutts · Dec 23, 2019 at 02:38 PM 0
Share

Thanks for replying! The only real thing that matters in the code is yield return new WaitForSeconds(35) StopAllCoroutines(); If I take it out the game crashes if I turn on the script. I want to move my code somewhere so the cloning script does not have that code I mentioned earlier but still functions in the same way, it clones for 35 secs and then stops all coroutines. Hope this helps, Thanks!

Show more comments
avatar image zacharif · Dec 23, 2019 at 02:39 PM 0
Share

Thank you Captain_Pineapple, I don't know how to format here, so thanks for helping me out so much!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Anis1808 · Dec 23, 2019 at 04:42 PM

In your other file create a List of coroutines that are running public List<Coroutine> all_coroutines;, then in each coroutine you start, add its instance to the list all_coroutines.Add(StartCoroutine(AnyCoroutine()));.

Finally, in the script you published, access the script in whatever way you want and create a foreach loop to stop the coroutines

 MySript script = // GetComponent or FindObjectOfType;
 foreach(Coroutine cor in all_coroutines){
      StopCoroutine(cor);
 }
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

161 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

Related Questions

how to only get the negative or positive part of a GetAxis 1 Answer

How do I stop my character from sliding after adding force? 0 Answers

Does anyone know how to activate different scripts OnTrigger? 1 Answer

How to clone on collision 0 Answers

3D / Top-Down - Make a script to let the player zoom the camera in and out 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