• 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 Anxo · Feb 27, 2014 at 08:25 PM · coroutinesbroadcastmessage

StartCoroutine() like BroadcastMessage() ?

I really like the way I can call, gameObject.BroadcastMessage("function","varible"); on to a game object and have it trigger in children.

Is there a way to do this with StartCoroutine? Right now I use the following work-arounds.

 SomeScript myscript = gameObject.GetComponent<SomeScript>();
 myscript.StartCoroutine("SomeFunction",someVariable);
 
 //or
 
 gameObject_B.BroadcastMessage("StartCoFunction", someVarible);
 //with on gameObject_B
 void StartCoFunction (SomeVariableType incVariable){
      StartCoroutine("MyCoroutine",incVariable);
 }
 IEnumirator MyCoroutine(SomeVariableType incVariable){
      yield return new WaitForSeconds(1);
     Debug.Log("Thank you for sending me "+ incVariable.name);
 }

But that is so much less convenient and messy than

 gameObject_B.BroadcastMessage("SomeFunction",someVariable);
 
 //On Gameobject B
 void SomeFunction(SomeVariableType incVariable){
     Debug.Log("Thank you for sending me "+ incVariable.name);
 }

It would be awesome if there was a way to send a Coroutine the same way as voids are triggered via BroadcastMessage, Kind of like a BroadcastCoroutine("CoroutineName",someVariable);

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

2 Replies

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

Answer by huntsfromshadow · Feb 27, 2014 at 10:03 PM

Hate to give you bad news, but their is not a way to do this.

You could extend all your personal classes and put your StartCoFunction in the parent class. Then all your classes would respond to the StartCoFunction method. Also since you can start a coroutine with it's string name you could just send the name of the coroutine to launch with the broadcast. You can pass along the two variables as an array or a struct.

Hope this helps.

Comment
Add comment · Show 4 · 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 Anxo · Feb 27, 2014 at 11:04 PM 0
Share

Hello Young Rouge,

What do you mean by this,

"Also since you can start a coroutine with it's string name you could just send the name of the coroutine to launch with the broadcast."

Do you mean to say that I make something like this?

 string[] stringArray = {"CoroutineName","importantPackage"};
 
 Broadcast$$anonymous$$essage("StartCoFunction",stringArray);
 
 void StartCoFunction(string[] incStrings){
      StartCoroutine(incStrings[1],incStrings[2]);
 }
 
 IEnumirator CoroutineName(string incString){
    yield return new WaitForSeconds(1);
    Debug.log("Thank you for sending me "+incString);
 }
 
 

Would this not limit me to only strings b/c I can not make an array with the first element being a string and the second element being a Vector3 or a Game Object, or can I?

avatar image huntsfromshadow · Feb 27, 2014 at 11:08 PM 0
Share

Yep exactly though I would remember arrays start at 0 in that code you'll get an our of index error.

avatar image Anxo · Feb 27, 2014 at 11:11 PM 0
Share

So it would limit me to strings?

avatar image huntsfromshadow · Feb 27, 2014 at 11:17 PM 0
Share

If you did it that way.

You could always do ins$$anonymous$$d an array of objects.

 object[] coroutineArray = {"CRName", 4}
 
 //Then when you deref you just have to know what the second argument was.
 void StartCoFunction(object[] incObjects){
      StartCoroutine(incObjects[0],incObjects[1]);
 }
  
 IEnumirator CoroutineName(object aValue){
    yield return new WaitForSeconds(1);
    Debug.log("Thank you for sending me "+(int)aValue);
 }

Now I'll warn you i've had unity sometime complain when I mix objects. You end up having to decide to use either system.object or UnityEngine.object, but that is just making sure your consistent.

avatar image
1

Answer by raybarrera · Feb 27, 2014 at 11:35 PM

This may be a bit outside the box, but I do think you're going about it the "wrong" way. In this case "wrong" is subjective, because if it compiles and it works, then it's not really wrong wrong.

However, there is a better design patter for this type of thing: Events. They're very powerful.

I'd recommend looking here for more information.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

Can I send a Transform as parameter with BroadcastMessage()? 1 Answer

Embed and communicate with Unity inside another app 2 Answers

Tesla Tower Trap 1 Answer

Is there an alternative to WaitForEndOfFrame without coroutines? 1 Answer

Concatenate two behaviours 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges