0
\$\begingroup\$

I'm trying to create an enable/ disable game objects in Unity. I used GameObject.SetActiveRecursively but it only works one-way.

I used a collider in which when an object enters the collider. The game objects become enabled. When they leave or get to a certain point, they disable.

How would I make this a two way system, making it able to be enabled while inside the collider and disabled when outside the collider?

-- The collider is in the game object who is being disabled and enabled.

According to this information from Unity Answers, the object becomes disabled. So how would I make the object enabled?

function OnTriggerEnter(other : Collider){
    if(other.tag == "Player"){
        gameObject.SetActiveRecursively(true);

    }
}

function OnTriggerExit(other : Collider){
    if(other.tag == "Player"){
        gameObject.SetActiveRecursively(false);
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Have you tried docs.unity3d.com/Documentation/ScriptReference/… \$\endgroup\$
    – Chuck D
    Jul 10, 2012 at 20:24
  • \$\begingroup\$ @Rubber Mallet Yes, that's how I formatted the script. But when I return to where the collider is (after I have exited), the game object doesn't return. I'm thinking to put the collider into a different object can use the script to call the other object. But I don't really know how to do that. \$\endgroup\$
    – Blue
    Jul 10, 2012 at 20:27
  • \$\begingroup\$ Can you post a bit of the code you're using to help get a better picture of what you're trying. Sounds like you have a trigger, an object that you want to dis/enable, and a player that walks into and out of the trigger to affect the other object? \$\endgroup\$ Jul 11, 2012 at 4:00

2 Answers 2

3
\$\begingroup\$

Let's say you have the following hierarchy:

  • ColliderAndScript
    • SetOfMeshes
    • AnotherSetOfMeshes

Attach your script and collider to ColliderAndScript. Add your things you want enabled/disabled as children. Then make your script like this:

function OnTriggerEnter (other : Collider) {
    if(other.tag == "Player"){
        for (var child : Transform in transform) {
            child.gameObject.SetActiveRecursively(false);
        }
    }
}

function OnTriggerExit(other : Collider){
    if(other.tag == "Player"){
        for (var child : Transform in transform) {
            child.gameObject.SetActiveRecursively(true);
        }
    }
}

This should enable all children when you enter the collider, and disable them when you exit.

\$\endgroup\$
2
\$\begingroup\$

If the collider is on an inactive game object, then of course it will never trigger, because it's not even in the game any more.

For something like this, the collider needs to be on a different object to the one you are switching on and off. Have the script on the collider object look up that other object to call SetActiveRecursively on it in both cases.

\$\endgroup\$
6
  • \$\begingroup\$ How would I do that? I don't know how to call other game objects(children) in the Heiarchy but only in prefabs from the project files. From what you are saying, I would have to create a parent object and a child object, and put the script in the parent while the child object is being turned on and off. Inside the script I would have to call the children and make set"SetActiveRecursively" to the child object. But as I said before, I don't know how to call the children of the a game object. Can you give me an example on how it is done? \$\endgroup\$
    – Blue
    Jul 11, 2012 at 12:06
  • \$\begingroup\$ This sort of thing is detailed in the Unity docs: docs.unity3d.com/Documentation/ScriptReference/… \$\endgroup\$
    – Kylotan
    Jul 11, 2012 at 14:42
  • \$\begingroup\$ I saw that but I don't understand it that well because I'm not using a script. I'm just turning on and off the child object \$\endgroup\$
    – Blue
    Jul 11, 2012 at 14:49
  • \$\begingroup\$ The code that does the "turning off" is in a script! \$\endgroup\$
    – Kylotan
    Jul 11, 2012 at 17:02
  • \$\begingroup\$ yes, but not the turn on \$\endgroup\$
    – Blue
    Jul 11, 2012 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .