Deactivate and Reactivate

Hi,

I’m trying to activate game objects that are hit by a given box collider on an object - so, if there is a collision the object gets activated, but for some reason, they are not being activated?

    public void ObjectComponentState(Collider2D other, bool state)
    {
        other.GetComponentInChildren<MonoBehaviour>(true).enabled = state;
        other.GetComponentInChildren<MonoBehaviour>(true).gameObject.SetActive(state);
        other.GetComponent<MonoBehaviour>().enabled = state;

     
    }

The above code is called when there is a collision enter and collision leave. I thought collisions still occur even if object isn’t activated so thought the above would work. Reason I’m doing this is because when objects are not in view, I don’t want them to be drawn.

Any help is much appreciated.

Thanks

no, inactive objects do not trigger, for all intensive purposes they don’t exist and should be treated as such. if you want to keep the collisions happening but everything else disabled put everything else into a child objects of the collision object and disable that, that way the collision code remains active while all the other/graphics disappear.

Thanks for this, but could you please elaborate on putting them into a child objects of the collision object?
Do you mean my game object now which has the collision into a child object?

anything you want deactivated put into a child object, and anything you don’t want deactivated leave in the parent. so you want your collision code to remain active so leave it on the parent, i assume any graphics/images you don’t want visible so move it to the child.

Current:
Gameobject + collider + image + more

After:
Gameobject + collider
→ Gameobject + image + more

Thanks, this makes sense. I have tried doing this, but still the child object doesn’t come active when it has hit my collision radius (i.e. in view). It is as though the parent object has also become in-active, this code should be called on enter/exit collision:

    public void ObjectComponentState(Collider2D other, bool state)
    {
        other.GetComponentInChildren<MonoBehaviour>(true).enabled = state;
        other.GetComponentInChildren<MonoBehaviour>(true).gameObject.SetActive(state);
        other.GetComponent<MonoBehaviour>().enabled = state;
    }

I basically fixed it, had to use this code to make child object active and inactive:

transform.GetChild(0).gameObject.SetActive(true);

while that works its better practice to set a public gameobject/transform in the script and reference that (and set via editor). using GetChild(0) will cause you many headaches later on if your hierachy changes for whatever reason

Yes, was thinking that myself. Thanks