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 /
avatar image
7
Question by PhoenixBlackReal · Sep 01, 2014 at 05:38 PM · coroutinestatic

How to start coroutine from another script?

Did my search and didn't quite find an answer. I'm making a countdown timer. Now - it works very neatly if I use start, update and IEnumerator and attach the entire script to the main camera. But I'd like to be able to have the ability to just have a simple call of the timer in another script. Something like Timer.Start_CountDown();

 public static IEnumerator Counter(){
     
     for(int a = 9; a >= 0; a--){
         if(a == 9){
             for(int x = 0; x <= 9; x++){
                     Ones_Number[x].GetComponent<SpriteRenderer>().enabled = false;
                     Tens_Number[x].GetComponent<SpriteRenderer>().enabled = false;
                 }
             Ones_Number[0].GetComponent<SpriteRenderer>().enabled = true;
             Tens_Number[9].GetComponent<SpriteRenderer>().enabled = true;
             yield return new WaitForSeconds (1f);
         }
         else
         {for(int i = 9; i >= 0; i--){
             for(int x = 0; x <= 9; x++){
                     Ones_Number[x].GetComponent<SpriteRenderer>().enabled = false;
                     Tens_Number[x].GetComponent<SpriteRenderer>().enabled = false;
                 }
                 Ones_Number[i].GetComponent<SpriteRenderer>().enabled = true;
                 Tens_Number[a].GetComponent<SpriteRenderer>().enabled = true;
                 yield return new WaitForSeconds (1f);
             }
             }
             
         }
     }

This is the timer base. I tried making a static void which would start the coroutine and that void would be called from another scrip, but it doesn't work.

I'm using c#.

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 ninja_gear · May 31, 2016 at 02:14 PM 0
Share

I$$anonymous$$O: $$anonymous$$eep your timer a non-$$anonymous$$onoBehavior class. Give it a constructor, a Time variable, a StartTimer function, and a CountDown coroutine. In any script that you need to use it, just create and save your Timer in that script on Start/Awake, and call StartTimer when you need to. If you want to get really fancy you can add a TimesUp EventHandler to the Timer, and have your controlling script subscribe to it. Good luck with your project.

1 Reply

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

Answer by zharik86 · Sep 01, 2014 at 06:58 PM

Create two scripts and attach them, for example, at Main Camera. The first script contains your timer with all variables:

  using UnityEngine;
  using System.Collections;
 
  public class myTimer : MonoBehaviour {
   //Create your variable
   ...
   //Your function timer
   public IEnumerator Counter() {
    ...
   }
  }

In the second script, for example, as Start () we will call the timer:

  using UnityEngine;
  using System.Collections;
 
  public class myCallTimer : MonoBehaviour {

   void Start() {
    //myTimer script attach at Main Camera, than
    StartCoroutine(Camera.main.GetComponent<myTimer>().Counter());
   }
  }

I hope that it will help you.

Comment
Add comment · Show 7 · 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 $$anonymous$$ · May 31, 2016 at 01:55 PM 0
Share

Thank you!!

avatar image Bunny83 · May 31, 2016 at 08:22 PM 11
Share

$$anonymous$$eep in $$anonymous$$d that this will run the coroutine on the gameobject where the "myCallTimer" script is attached to, not on the main camera. As long as the gameobject and the script aren't destroyed there's no problem, it just can lead to problems in cases where the calling script / object is destroyed.

If you want to run the coroutine on the "myTimer" script you should do:

 void Start() {
     myTimer obj = Camera.main.GetComponent<myTimer>();
     obj.StartCoroutine(obj.Counter());
 }

ps: Classnames (in almost any program$$anonymous$$g languages) should start with an upper case letter.

avatar image micheal_s Bunny83 · Mar 11, 2019 at 08:35 PM 0
Share

You helped me so much thank you god bless <3

avatar image Saki_col Bunny83 · Aug 08, 2019 at 07:07 PM 0
Share

good to know it thanks!

avatar image gitlinjoss Bunny83 · Apr 21, 2020 at 08:22 PM 0
Share

exactly what I needed chief!

Show more comments
avatar image KarlKarl2000 · Jul 30, 2016 at 04:42 PM 2
Share
 StartCoroutine(Camera.main.GetComponent<myTimer>().Counter());

That was it! Thank YOU :)

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

30 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

Related Questions

Static and Coroutines help 2 Answers

Call methods on start of Application 2 Answers

How to make an HTTP request in a menu item ? 2 Answers

How can I instantiate a prefab from a static function? 1 Answer

Static Coroutine being called endlessly 2 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