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
5
Question by Captain_Dando · May 09, 2013 at 08:25 AM · prefabcoroutinesienumerator

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

Hi everyone, this question is a bit long, but please bear with me.

I'm creating a game with a combat system very similar to final fantasy 10. I'm a big fan of keeping my code as neat as possible, so my characters all derive from a basic character class. that class contains a List of four abilities (as each character can only have four), which is of the type basicSkill, like this

     List<basicSkill> Skills = new List<basicSkill>();

I then created an empty prefab named Attack (for a basic attack) with only basicSkill as it's component. I then dragged this prefab into the List of skills in a characters' component.

within every skill is a Run() function. it's always named run so that no matter what skill it is, you can run it straight from the character's skills list by it's index in the list.

Run is seen below

     public void Run (GameObject attacker, GameObject target, _DefaultCharacter attackerStats, _DefaultCharacter targetStats){
         _damage = attackerStats.Damage / targetStats.armor;
         if (isMelee == true){
             StartCoroutine(MeleeSequence());
         }
     }

     IEnumerator MeleeSequence(){
         yield return new WaitForSeconds(3);
         Debug.Log(name + "Sequence Complete");
     }

Once I had that sorted out, I created a button in order to test out my attack by having first character and enemy in the list as parameters. I was able to run attack through a long and convoluted line which you can see below.

 if (GUI.Button(new Rect(10,10,50,20), "test attack")){
             allies[0].GetComponent<_DefaultCharacter>().newSkills[0].Run(allies[0], enemies[0], allies[0].GetComponent<_DefaultCharacter>(), enemies[0].GetComponent<_DefaultCharacter>());
         }

The problem is, I keep getting the error seen below;

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

I believe that the problem lies in the fact that it is a prefab that I drag to the inspector window on skills. when I have the same prefab in the scene and try to drag it into the inspector it won't allow it, though I don't see why it would accept it as a prefab. The whole reason I've done this is because I've tried to stop using dynamic calls, though I don't have any other ideas.

What might be causing the error?

Comment
Add comment · Show 4
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 Fattie · May 09, 2013 at 09:37 AM 1
Share

Hmm, it looks like the coroutine couldn't be started, because the the game object 'Attack' is inactive.

avatar image Captain_Dando · May 09, 2013 at 09:50 AM 0
Share

That does appear to be the case

avatar image Captain_Dando · May 09, 2013 at 09:56 AM 0
Share

I'm new to both program$$anonymous$$g and unity. I'm sorry if the error seemed obvious, but it really isn't to someone with as little experience as me. I didn't know that you can't run functions or coroutines from a prefab that hasn't been added to the scene so "Inactive" could mean any number of things to me

avatar image Fattie · May 09, 2013 at 01:09 PM 0
Share

for beginner tutorials unityGE$$anonymous$$S.com

1 Reply

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

Answer by Bunny83 · May 09, 2013 at 09:18 AM

Coroutines are like you might know objects and not functions. Those "objects" have to be processed by Unity's coroutine scheduler. Coroutines are always bound to a MonoBehaviour which will hold a reference internally to all active coroutines and are responsible for processing them. If you call StartCoroutine you just pass the coroutine object to the scheduler. Of course the MonoBehaviour needs to be an active scene object in order to schedule coroutines.

So you should either:

  • instantiate your skill prefabs as childs of your player (maybe in the characters Start function)

  • Run the coroutine on a different Monobehaviour.

It seems your _DefaultCharacter class is a MonoBehaviour which is actually an instance in the game. You can run the coroutine on this MonoBehaviour:

 attackerStats.StartCoroutine(MeleeSequence());
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 bdgreen88 · Mar 29, 2017 at 01:30 AM 2
Share

Co$$anonymous$$' to you from the future Bunny83 to give a big big thanks. Looked forever to find out that coroutine has to be run on a $$anonymous$$onobehaviour. $$anonymous$$an I wish this thread was more popular and that I understood this crap more.

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

15 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

Related Questions

My game freezes whenever i use a function which involves http? 0 Answers

Coroutines IEnumerator not working as expected 2 Answers

Generating Objects at High Speeds 0 Answers

How to randomly spawn object without spawning the previous object in a row? 0 Answers

WaitUntil doesn't work and the coroutine starts anyways 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