• 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 edincanada · Oct 21, 2016 at 12:53 PM · coroutinemonobehaviourapigamobject

StartCoroutine can't be called from a GameObject. Am I late to this party?

I recently realized that I can't call StartCoroutine from a gameObject. Instead, I need a reference to a monobehaviour that is attached to the desired game object.


When was t$$anonymous$$s API changed made? I particularly dislike it for two main reasons. 1) Coroutines are bound to gameObjects, not monobehaviours. Disabling the monobehaviour that started the coroutine won't affect the coroutine at all. Disabling the gameObject does.


2) If I want to start a coroutine from a reference to a gameObject other than the current, I have to either a) hope that gameObject already has a monobehaviour, or B) add a new dummy monobehaviour myself. T$$anonymous$$s can be annoying if the referenced gameObject isn't always the same. Creating mono behaviours may trigger garbage collection.


So, why doesnt the API just let us do GameObject go; go.StartCoroutine( coroutine()) ;


Having to find (or create) a mono behaviour on the reference gameObject is almost as nonsensical as having to find (or create) a monobehaviour to activate or deactivate a gameobject.

Comment
Add comment · Show 2
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 Owen-Reynolds · Oct 21, 2016 at 04:50 PM 0
Share
avatar image Bonfire-Boy Owen-Reynolds · Oct 21, 2016 at 05:01 PM 0
Share

2 Replies

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

Answer by Bonfire-Boy · Oct 21, 2016 at 01:11 PM

I'm not sure what you mean by "can't call StartCoroutine from a gameObject". Of course you can. Yes, the GameObject has to have a MonoBehaviour in w$$anonymous$$ch to do it. "Having to create a MonoBehaviour on the gameObject," doesn't make sense to me. Without a MonoBehaviour, a GameObject has no behaviour and can only be manipulated by other behaviours. MonoBehaviours are how we get GameObjects to do stuff, it's hardly a restriction.

Or is it just that you've just got the code wrong? From one MB you can start a coroutine in another, but you do it using

 ComponentType c = go.GetComponent<ComponentType>();
 StartCoroutine(c.coroutine());

Being able to do t$$anonymous$$s gives us more control options (the coroutine dies with the calling object).

I appreciate I may be missing somet$$anonymous$$ng. If so, maybe a bit more code and a fuller description of your intended set-up might help.

[Edit] See comments below for further discussion of the situation including answers to my questions and resolution.

Also, w$$anonymous$$le t$$anonymous$$s accepted answer and thread deals with the "what are you even trying to do here and why?" side of t$$anonymous$$ngs, and resolves it, the other answer by Bunny83 correctly addresses the "change of API" aspect of the question.

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 edincanada · Oct 21, 2016 at 02:26 PM 0
Share
avatar image Bonfire-Boy edincanada · Oct 21, 2016 at 02:34 PM 1
Share
avatar image Bonfire-Boy Bonfire-Boy · Oct 21, 2016 at 02:56 PM 1
Share
Show more comments
avatar image edincanada · Oct 21, 2016 at 03:14 PM 0
Share
avatar image
2

Answer by Bunny83 · Oct 21, 2016 at 03:14 PM

There hasn't been a change of the API. You were never able to run a coroutine on a GameObject. Coroutines run on the MonoBehaviour instance of w$$anonymous$$ch you use StartCoroutine. Your conclusions based on your observations are actually wrong.

First of all the "enabled" state of a MonoBehaviour only controls if FixedUpdate, Update, LateUpdate, OnGUI are called or not. All other callbacks still work fine when the script is "disabled". That includes coroutines. So disabling a MonoBehaviour won't have an effect on running coroutines and won't prevent you from starting new coroutines.

Try running a coroutine on a MonoBehaviour instance and destroy only the MonoBehaviour instance. The coroutine will be removed as well, even the gameobject is still there.

Deactivating a GameObject on the other hand will "remove" the gameobject from the whole scene. Not in the sense of wiping out the memory but it will have no interactions with any other gameobject. From the scene's point of view the gameobject doesn't exist. Deactivated gameobjects won't receive any messages by Unity and it does cancel running coroutines. Also if you try starting a coroutine on a script of a deactivated gameobject you will get an error that says:

Coroutine couldn't be started because the the game object 'XXX' is inactive!

So not$$anonymous$$ng has changed and it still works the same as in Untiy version 2.6 (where i started using Unity).

GameObjects are just containers. They don't have any functionality themselfs. It's the components w$$anonymous$$ch adds behaviour and other t$$anonymous$$ngs. A GameObject is only responsible for providing:

  • a container for Components

  • provide fundamental state informations of the object such as: name, tag, layer, containing scene, static flag and the active state.

  • An API to access and add components

  • An API to send messages to attached components.

A Gameobject doesn't even have a position. That's handled by the Transform component (w$$anonymous$$ch however is always with a GameObject).

Comment
Add comment · Show 6 · 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 edincanada · Oct 21, 2016 at 03:18 PM 0
Share
avatar image Bonfire-Boy edincanada · Oct 21, 2016 at 03:25 PM 0
Share
avatar image edincanada Bonfire-Boy · Oct 21, 2016 at 03:28 PM 0
Share
Show more comments
avatar image Bunny83 edincanada · Oct 21, 2016 at 03:31 PM 0
Share

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Which monobehaviour functions can be defined as coroutine (except Start) 2 Answers

Can coroutines be used in external C# dlls? 1 Answer

Coroutine without MonoBehaviour 6 Answers

Execution order when Start is declared with IEnumerator return (as a Coroutine) 1 Answer

Yield return null? 3 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