Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NateJC · Jun 11, 2014 at 06:03 PM · c#coroutinestringchildstop

StartCoroutine of a child object using String

I want to start a coroutine of another script from a driving script. This works fine in C# like this:

 StartCoroutine(mOtherObject.SomeCoroutineFunc());

The problem is that I also need the ability to Stop this coroutine, which only works if the StartCoroutine uses the String form of initialization. So, how do change my StartCoroutine call to work in this case? The following does not work:

 StartCoroutine("mOtherObject.SomeCoroutineFunc");

Yes, I know that I could just call mOtherObject.BeginCoroutineFunc() which in turn would start the coroutine with a String. But this is not my question. Specifically, I want to know:

Is there a way to use the C# string form of coroutine initialization of another object besides intself?

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
3
Best Answer

Answer by Landern · Jun 11, 2014 at 06:25 PM

Perhaps a better idea would be to use SendMessage to start the Coroutine in the accepting components.

Comment
Add comment · Show 3 · 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 NateJC · Jun 11, 2014 at 06:46 PM 0
Share

Thanks, but I don't understand how that syntax would look. Obviously, this won't work:

 mOtherObject.Send$$anonymous$$essage("StartCoroutine(SomeCoroutineFunc)");

How would you set it up?

avatar image Key_Less · Jun 11, 2014 at 07:17 PM 3
Share

If you really need to use a string, you could pass the string name of the coroutine as a parameter with your message. It would look like this:

 mOtherObject.Send$$anonymous$$essage("Start$$anonymous$$yCoroutine", "SomeCoroutineFunc");

Then inside of your mOtherObject script...

 public void Start$$anonymous$$yCoroutine(string coroutineName)
 {
    StartCoroutine(coroutineName);
 }

However, if you're using Unity 4.5, StopCoroutine can now take an enumerator argument. I have not used/tested this, but in theory this would work:

 StartCoroutine(mOtherObject.SomeCoroutineFunc());
 StopCoroutine(mOtherObject.SomeCoroutineFunc());
avatar image NateJC · Jun 11, 2014 at 09:47 PM 0
Share

Oh nice! I didn't know about Unity 4.5 StopCoroutine now taking an enumerator argument! I'll be updating right now. Thanks! :o)

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Instantiate from correct Icon. 1 Answer

Coroutines randomly stop working 0 Answers

string.TrimEnd() not working as expected 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