Colliding with multiple objects to destroy other gameObject

Hey guys,

So i recently made a script for destroying a gameObject (a door) when the player collides and destroys a different gameObject (a key). Basically acting like a key and door system. While this works great, it also heavily limits my level designs since it only requires one collision to get the door destroyed. What i need is a script that requires all keys in a level to be destroyed/collided with in order for the door to get destroyed. I’m thinking that the easiest way to do this is to have a script check if there are any keys left in the scene, and if keys = 0, the door will get destroyed. I’m sure there are other ways too.

Below is the script currently attached to the “key” object.

JS:

var thing : GameObject;

function OnCollisionEnter (col : Collision) {

if(col.gameObject.tag == "Player")
{
    Destroy(thing.gameObject);
}

}

I also have a separate script for destroying the key on collision with the player. That script is attached to the player and looks like this:

using UnityEngine;
using System.Collections;

public class DestroyCube : MonoBehaviour
{
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == “Key”)
{
Destroy(col.gameObject);
}
}
}

Any help appreciated. Thanks.

Each key can have a reference to the door. The door has a script with a public int numKeys. then on the keys:

if(col.gameObject.tag == "Player")
 {
     door.numKeys--;
 }

Then in Update() on the door:

if (numKeys <= 0)
{
    Destroy(gameObject);
}

You could use tags for your keys. Each key GameObject would be set with the ‘key’ tag and when you collide with the door simply use FindGameObjectsWithTag("key") and if the result length is > 0 you know there are still keys.

You could also make a more complex system that strictly link the keys necessary to the door that needs them and the door would check itself to see if the keys are still there. That way you could have more than one door, each requiring a different set of keys.

You could put something like this on the door GameObject:

OnColliderEnter(Collider other){
    if(other.col.gameObject.tag == "Player" && GameObject.FindObjectsWithTag("key").length == 0) {
        Destroy(this.gameObject);
    }
}

And of course be sure the set the tag for the key GameObjects to the proper tag.

@jinkata please elaborate on your first solution with FindGameObjectsWithTag, im still very new to this. Thanks!

Hey Mate. Here is the code as best I could Manage, fixed syntax errors. If you still have errors let me know.

Key Code

using UnityEngine;
    using System;
    
    public class Key : MonoBehaviour {
    // In the inspector drop and drag the linked door object here
     public Transform linkedDoor; 
     doorScript ds;
     
    void Start () {
           ds = linkedDoor.GetComponent<DoorScript> ();
           ds.AddKey (transform);
     }
     
    void OnCollisionEnter (Collision other) {
       // Remove the Key
       ds.RemoveKey (transform);
     
    // Destroy this key
       Destroy (this.gameObject);
     }
    }

DoorScript Code

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DoorScript : MonoBehaviour {
 
   List<Transforms>  keyList;
 
   void Start () {
      keyList = new KeyList ();
   }
 
   public void AddKey (Transform key) {
       keyList.add (key);
   }

    public void RemoveKey (Transform key) {
       keyList.Remove (key);
 
      if (keylist.Count == 0) {
          Destroy (this.gameObject);
       }
    }
 }