Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Children of an instantiated object will not set active

Discussion in 'Scripting' started by lemonrays, Apr 29, 2016.

  1. lemonrays

    lemonrays

    Joined:
    May 7, 2015
    Posts:
    48
    This is my script, which is attached to instantiated objects. PlayThis() is referenced by another script.

    "bob" is a child object and is successfully deactivated at Start (it is active in the prefab, and properly associated), but it never activates in PlayThis(). The debug message "AAAAAAAAAA" successfully shows up, however. I am confused as to why the child never activates. I am positive that this has something to do with the fact that it is an instantiated object, but I'm not sure how to fix it, because I have no console errors.
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    just because you set the parent active to false doesn't mean the children go false. You need to go through each child and set it to false if you want. You use to be able to just say SetActiveRecursively, but that's been depricated :/
     
  3. lemonrays

    lemonrays

    Joined:
    May 7, 2015
    Posts:
    48
    ...huh? that doesn't help me lol
    what I'm trying to say is, nothing is happening to the child in that particular void no matter what it is, fiddling with what's active or inactive in the heirarchy or the prefab itself doesn't seem to change anything
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    May have mis read something...If you're trying to call PlayThis() on an inactive object, you can't.

    Make sure you have a handle on to that instantiated object?

    Show us how you are instantiating
     
  5. lemonrays

    lemonrays

    Joined:
    May 7, 2015
    Posts:
    48
    No no, I promise it's active lol.
    Essentially, I'm spawning enemies, they're prefabs, they spawn, they attack, there is a child that's active in the prefab, but set to inactive in the "start" because I want it to activate on demand, but it will not listen to me. Myeh. I don't know if I need to use awake instead? I just dunno.
    The line in my other script:
    Instantiate(prefab1,spawn.transform.position,spawn.transform.rotation);
    "spawn" is an empty game object that sets a spawn position.
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    okay, so to make sure I got this straight,

    You spawn a prefab. prefab1 in this case... Inside that prefab is a child gameObject with a script you'd like to call a method in. Is that correct?

    Maybe it's just me. Your problem isn't very clear. Maybe @lordofduct can help out.
     
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    If you have a script or child object that's apart of the gameObject you are spawning, then you need to instantiate it like this:


    Code (CSharp):
    1. GameObject go = Instantiate(prefab1) as GameObject.
    2.  
    3. Then you can type:
    4.  
    5. go.PlayThis();
     
  8. lemonrays

    lemonrays

    Joined:
    May 7, 2015
    Posts:
    48
    You may be onto something lol. I'll let you know if this works.
     
  9. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    just fyi, that was basic. You can't actually type go.PlayThis() you'll need to access the component first.

    go.getcomponent<YourScript>().PlayThis()

    Might want to store it in a local variable and then also check if it's null.