Search Unity

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

Discussion in 'Scripting' started by deLord, Oct 21, 2014.

  1. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    I have a CastManager that gets notified when the player cast a spell.
    Code (CSharp):
    1. public void setTarget(GameObject targ){
    2.      ((ProjectileSpell)currSpell).target = targ.transform.parent.gameObject;
    3.     }
    After this has happened, in the Update() function, cast() in the currSpell is called. cast() then does this:
    Code (CSharp):
    1. modelInstance = (GameObject)Instantiate(model, source.transform.position + new Vector3(0.4f, 0.6f), Quaternion.identity);
    2. startMovement(GameObject.Find(target.name+"/DefaultTarget").transform);
    startMovement is in ProjectileSpell which is the super class of SimpleFireballSpell:
    Code (CSharp):
    1. /** Moves to model into the direction of targ */
    2. public void startMovement(Transform targ){
    3.     isTravelling = true;
    4.     StartCoroutine(moveToTarget(targ));
    5. }
    6.    
    7. IEnumerator moveToTarget(Transform targ) {
    8.     while(Vector3.Distance(modelInstance.transform.position, targ.position) > 0.1f) {
    9.             modelInstance.transform.position = Vector3.MoveTowards(modelInstance.transform.position, targ.position, speed * Time.deltaTime);
    10.             yield return null;
    11.         }
    But after the instantiation, my fireball doesn't move. The error at runtime is
    I debugged the code while running and in public void startMovement, the target was filled but when the coroutine is called, target is null ô.O can someone elaborate on this? Thanks in advance :)
     
    Volchok likes this.
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    In order to run a Coroutine on SimpleFireballSpell it has to be active. Make it active before you start the coroutine.
     
    Bunny83 likes this.
  3. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    But how can it be not active if it gets rendered in the scene? I mean, my prefab looks like this:
    New Empty
    > SimpleFireballSpell script
    > Model and Particle system

    When it is rendered in the scene, it contains all 3 components. Is there anything I missed about Instantiate()? I assume that all Components have the active status as I defined in the prefab.
    Also, when debugging, it is active and enabled :-?
     
    Last edited: Oct 21, 2014
    Volchok likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Insert, between lines 3 and 4, a check to see if the object is active:
    Code (csharp):
    1.  
    2. Debug.Log("Active? "+gameObject.activeInHierarchy);
    3.  
     
  5. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    It says false. I dont understand that O.O
    Why is that? And how can I change it? Sorry if this sounds noob but I must have missed sth
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,700
    Insert a Debug.Break() statement in right before you attempt the coroutine spawn.

    When that statement triggers the Unity game engine will still be running but paused and you can go inspect the object in your hierarchy and make sure the object is SetActive and also the component containing the coroutine is enabled.

    Debug.Break() is really awesome for stuff like this because you get control of the system precisely where you need to check it.

    Kurt
     
    hopetolive likes this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, almost precisely. In a traditional break (programming-wise), code execution stops at the actual line on which the break is placed. You can then step through line by line and watch local variables change each line, and so on.

    Debug.Break() is different in that it pauses execution at the end of the frame. So you'll get the correct frame, and it's useful for debugging this sort of thing, it's just important to note that that isn't what "break" usually means.

    (Unity + MonoDevelop does support a traditional breakpoint as well, if stepping through lines is what you actually need.)
     
  8. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    But as I mentioned earlier, when I debug, before calling the coroutine targ will be filled while when I step INTO the coroutine, it is null right as a parameter. I don't know what more I can debug here.
    So why is activeInHierarchy false while enabled and active are true in all lines when I debug?

    Maybe I should notice once more that I am a Unity beginner and maybe I miss sth about Instantiate(), "activation/enabling" or coroutines. If this is an easy problem (and I am sure it is nothing fancy), please just explain for dummies and/or post the line of code I need. I really appreciate your help guys :)

    Another remark: I can not activate or deactivate my script in my prefab. Can this have something to do with this?
     
    Last edited: Oct 22, 2014
  9. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Are you calling StartCoroutine from an object inheriting monobehaviour?
     
  10. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    StartCoroutine() is called from ProjectileSpell which indirectly inherits from MovoBehaviour, yes.
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Something is making SimpleFireballSpell inactive. It could be inactive in the prefab or some other code could be making it inactive. The solution is to ensure it's active while the coroutine is running. Beyond that there isn't much more assistance to provide.
     
  12. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    Seems I made something wrong when setting up the prefab. How to fix this?

    A button in the scene evokes a fireball being cast. I want to connect my spell with that button. But what I did is to create a prefab that has this script in it. But my CastManager takes a Spell. How can this even work? I can connect a prefab (new empty type GameObject) with a function in my CastManager that takes a spell?

    So, this is the problem and I wired it totally weird. I tried for hours but cant get the right approach.

    How would one handle this? Button calls a function that takes a Spell and then delegates it to the more specific Spell (e.g. SimpleFireballSpell). This specific spell should know the prefab.

    SimpleFireballSpell : ProjectileSpell : Spell : MonoBehaviour
     
    Last edited: Oct 22, 2014
    benzsuankularb likes this.
  13. benzsuankularb

    benzsuankularb

    Joined:
    Apr 10, 2013
    Posts:
    132
    Did you found the issue?
    I just met this. the gameobject active ticked. Component enable ticked. But don't know why?
    activeInHierarchy false.

    Update : I just found the issue, For those who stuck with this.
    The GameObject just have been created it passed Awake, but didn't pass Start method yet.
    Occur when I instantiate from a prefab and do StartCoroutine immediately.

    However this is strange...
     
    Last edited: Dec 6, 2016
  14. UpheavaI

    UpheavaI

    Joined:
    Dec 19, 2015
    Posts:
    1
    Hey! deLord, or anyone. if this happens , where it says "object is inactive" make that object a prefab, delete it. drag the prefab on and it should be fixed. it happened to me!! your welcome ~
     
  15. SuperDamien

    SuperDamien

    Joined:
    Mar 27, 2015
    Posts:
    1
    Thank you. I had an altogether similar issue and this fixed it. Really made my evening!
     
    LiveOrDevTrying likes this.
  16. Cowsep

    Cowsep

    Joined:
    May 31, 2017
    Posts:
    5
    Adding another solution for a similar problem.

    If you're using a Singleton that is a prefab, make sure you're calling the instance of the prefab otherwise it won't be using the active Singleton but the prefab instead (which is not active in the scene)
     
  17. FyndNorway

    FyndNorway

    Joined:
    Sep 4, 2018
    Posts:
    18
    How can I know which instance I'm calling on?
     
  18. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When you instantiate the object into the scene, you have to grab a reference to it then. If you don't do that, you're probably calling on the prefab.
    Code (csharp):
    1. public GameObject somePrefab;
    2. void Start() {
    3. GameObject someInstance = Instantiate(somePrefab);
    4. SomeScript ss = someInstance.GetComponent<SomeScript>();
    5. ss.StartCoroutine(ss.SomeCoroutine() );
    6. }
    Generally, for any public GameObject references in my scripts, I'll add "prefab" to their name if I intend them to be prefabs. That helps keeps the objects in scene separate from the prefabs.
     
  19. sean244

    sean244

    Joined:
    Nov 4, 2017
    Posts:
    95
    That worked for me! As StarManta correctly pointed out: The problem is that you're referencing the actual prefab itself - which is not active in the scene. You need to create a gameobject and set that to equal the instantiated prefab. Something like
    GameObject go = Instantiate(prefab);
    go.GetComponent<SomeClass>().SomeCoroutine();
     
    RohitNutalapati likes this.
  20. abishekpoudel2014

    abishekpoudel2014

    Joined:
    Mar 20, 2019
    Posts:
    2
    That didnt work for me do you have any other solutions
     
  21. abishekpoudel2014

    abishekpoudel2014

    Joined:
    Mar 20, 2019
    Posts:
    2
    Hello my problem is that it says that my gamemanager is inactive but it is active.
     
  22. fahimsiddiqui

    fahimsiddiqui

    Joined:
    Dec 22, 2018
    Posts:
    1
    THIS MAN IS A GOD. I was stuck in this because unity is dumb. I referenced it in the inspector but the gameobject automatically kept connecting to the prefab because i unpacked the one in the hierarchy. Re-referencing it helped:D
     
  23. unity_T4fQ0JU7B0A2Pg

    unity_T4fQ0JU7B0A2Pg

    Joined:
    Jan 21, 2019
    Posts:
    2
    I have just found solution for this issue.
    OnClick () was referencing to a prefab, that was not instantiated
    try to reassign OnClick () prefat to gameobject, that is already in scene
     
    Igo_Go likes this.
  24. scrumnier

    scrumnier

    Joined:
    Jan 8, 2022
    Posts:
    1
    I just see this in players interface: UI with script "Panel", prefab is alredy exist and Corutines works, but only when i start it from this script. From other scripts - i catch exeption. I try to Instantiane - and exception solved, but interface - duplicated
    Code (CSharp):
    1. public static Panel panel;
    2. ....
    3. //in Start()
    4. panel = Intstaniate(this).GetComponent<Panel>();
    than i just use script
    //in Start()
    Code (CSharp):
    1. panel = GetComponent<Panel>();
    2. ....
    3. //in action
    4. panel.StartCoroutine(panel.PassiveSkillExecuteAnimation(_buttonPassiveSkill[1]));
    5.  
    may be somebody will be helpfull
     
  25. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,700
    Maybe you will make your own new post rather than hijacking some random post from 2014.

    When you post, remember, NOBODY can read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - links to documentation you used to cross-check your work (CRITICAL!!!)
    - what actually happened, especially any errors you see

    If you post a code snippet, NEVER RETYPE! Always copy paste. For instance, this is completely misspelled nonsense:

    When you post code, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Start your own new thread.
     
    KReguiegReply and Bunny83 like this.
  26. danieltanfh95

    danieltanfh95

    Joined:
    Mar 12, 2019
    Posts:
    2
    Got this again. The issue was that when opening prefab mode, I dragged in a prefab and referred to it, but I pressed start and it prompted to exit prefab edit mode. Selected yes to exit made Unity bugged out and selected the prefab instead of the actual instantiated version in the scene.

    TLDR: go to the scene and inspect what's actually referenced. drag it in the scene and apply changes to the prefab.