help with SetActive

I am trying to make a very simple 2D game where you collect keys to open a door but i am struggling with the door.

I added the door into the game and added this code:

void OnTriggerEnter2D(Collider2D other)
	{
		if(other.gameObject.tag == "doorClosed" && keys == 2)
		{
			other.gameObject.SetActive (false);
		}
	}

Everything worked apart from the player could simply walk through the door, and if the they had the 2 keys then they would be able to walk through the door and make it disappear(what i wanted to do).

To fix the problem i created a new empty game object and added a colider to it and placed it over the door. Everything worked but obviously you couldn’t even walk through the door even with the keys.

I tried putting doorClosedCollider.SetActive (false); in the if statement but it said it was not in the current context.

Please could someone tell me the correct code, and how it works.

Thanks for the help and being patient enough to read through all that!

OnTriggerEnter2D works if the collider on the other object is marked as a trigger.

Triggers activates their OnTriggerEnter2D methods when other colliders hit them, but they DON’T stop movement. If you want the door to also stop the player from moving through it, the door has to no be a trigger. Select the door object, find the BoxCollider2D (or whatever collider you use, and switch off the trigger checkbox.

Now, as the door is not a trigger anymore, the OnTriggerEnter2D method won’t be activated anymore. Replace it with OnCollisionEnter2D, which handles collisions between non-triggers.

Finally, your player has to have both a collider and a rigidbody for the collision to enter.

To sum up:

  • the player needs to have a rigidbody2D and a collider that’s not a trigger

  • the door needs to have a collider that’s not a trigger

  • the player needs the script you have above, but with OnTriggerEnter replaced by OnCollisionEnter, like this:

    void OnCollisionEnter2D(Collision2D coll) {
    if(coll.gameObject.tag == “doorClosed” && keys == 2)
    {
    other.gameObject.SetActive (false);
    }
    }

I’d suggest starting here: http://unity3d.com/learn/tutorials/modules/beginner/2d/physics2d to figure out more about how colliders and such works in Unity 2D.

You only need to use a single collider, and it doesn’t need to be a trigger. Something like this on your door should work

public class Door : MonoBehaviour {

    public float keys = 0;
    public GameObject openDoorPrefab;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            if (keys == 2)
            {
                gameObject.SetActive(false);
                Instantiate(openDoorPrefab, transform.position, Quaternion.identity);
            }
            else
            {
                Debug.Log("You don't have all the keys!");
            }
        }
    }
}

When there’s a collision with the door you check to see if it’s the player. If it is the player then you check if all the keys are collected, and if so disable the door game object and then instantiate a prefab of the open door. You just need to make sure you tag your player as “Player” in the editor.

EDIT: You probably want to keep track of the keys in the player script, this is just an example!