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
1
Question by Juneyee · Jul 30, 2017 at 10:45 AM · unity 5coroutinesienumeratorparameters

Can I use StartCoroutine(CoroutineName()) instead of StartCoroutine("CoroutineName") from another script?

So what's going on is that I have a script with the below Coroutine:

 public IEnumerator ThingHandler(float input, bool active)
 {
     yield break;
 }

And I'm trying to call it from another script using StartCoroutine, but because I have 2 parameters I can't use

  StartCoroutine("CoroutineName");

and instead I'm forced to use

  StartCoroutine(ThingHandler(2, true));

as the first method does not allow me to pass in 2 parameters.

Using

  GetComponent<FirstScript>().StartCoroutine ...

from another script does not allow me to use the non-string method of calling the coroutine as it returns an error:

  The name 'ThingHandler' does not exist in the current context.

I'm not sure what I can do at the moment, should I be adding a new function under the first script and start the coroutine from there instead? Or is there a more elegant way of solving the issue? Am I doing anything obscenely wrong?

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 CorruptedTNC · Jul 30, 2017 at 11:59 AM

I had to make an assumption here, which was that both your scripts inherit from Monobehaviour and are attached to a GameObject.

I think you're getting confused about the context that StartCoroutine should be used in. StartCoroutine is just a method which you can call and supply it with the coroutine you want to use.

You were on the right track with your attempt, pointing your 2nd script towards the first, but StartCoroutine should be the first thing you call.

 public class CoroutineContainer : MonoBehaviour
 {
     public IEnumerator ThingHandler(float input, bool active)
     {
         Debug.Log($"You called me with {input}, and {active}");
         yield break;
     }
 }

I've included a Debug.Log line in a fake script containing the coroutine, so that you can verify this works if you want to play around some more.

 public class Caller : MonoBehaviour
 {
     public CoroutineContainer coroutineContainer;
 
     private void Awake()
     {
         StartCoroutine(coroutineContainer.ThingHandler(5.0f, true));
     }
 }

The reference to CoroutineContainer is more than enough to start off our coroutine. When putting Caller on a gameobject, you'll need to specify which GameObject in your scene contains the CoroutineHandler in the inspector.

Another alternative for the caller is to have the following:

 public class Caller : MonoBehaviour
 {
     public Transform coroutineObject;
 
     private void Awake()
     {
         var coroutineContainer = coroutineObject.GetComponent<CoroutineContainer>();
         StartCoroutine(coroutineContainer.ThingHandler(5.0f, true));
     }
 }

This option tends to be more useful if objects are likely to disappear or be changed frequently, so the reference that you add manually won't be valid after some time.

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 Juneyee · Jul 30, 2017 at 06:11 PM 0
Share

Perfect. I simply confused myself by comparing StartCoroutine to directly calling a function in the other script.

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

134 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

Related Questions

How can I spawn different GameObjects using IEnumerator? 1 Answer

How do I pass a parameter for trigger animations in the animator controller unity 5 file 0 Answers

How to make animation state follows float paramets? 0 Answers

How can I access Animator parameters in C# 2 Answers

Serializable class with coroutines? 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