Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
    }
}
share|improve this question
    
Have you tried docs.unity3d.com/Documentation/ScriptReference/… –  Rubber Mallet Jul 10 '12 at 20:24
    
@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. –  Blue Jul 10 '12 at 20:27
    
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? –  chaosTechnician Jul 11 '12 at 4:00

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
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? –  Blue Jul 11 '12 at 12:06
    
This sort of thing is detailed in the Unity docs: docs.unity3d.com/Documentation/ScriptReference/… –  Kylotan Jul 11 '12 at 14:42
    
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 –  Blue Jul 11 '12 at 14:49
    
The code that does the "turning off" is in a script! –  Kylotan Jul 11 '12 at 17:02
    
yes, but not the turn on –  Blue Jul 11 '12 at 18:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.