- Home /
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.
Answer by Jwizard93 · Jul 11, 2017 at 01:50 AM
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);
}
Hi, i am very new to this. Can you explain a bit more in detail how to do it? Where to attach the scripts and so on. Thanks!
Either also destroy the key upon the collision with player, or use a bool so that it will only decrement num$$anonymous$$eys once. Like
if(col.gameObject.tag == "Player")
{
Destroy(gameOJect);
door.num$$anonymous$$eys--;
}
OR
private bool hasBeenPickedUp = false;
function OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Player" && !hasBeenPickedUp)
{
door.num$$anonymous$$eys--;
hasBeenPickedUp = true;
}
}
This might be C# and Unityscript mixed together but you get it.
@jwizard93 i really appreciate your help, but no, i dont get it. Are the scripts written in c#? I get this error: error CS1525: Unexpected symbol `if'
I think i will need a full script to be able to understand this, i am still a beginner.
Answer by Jinkata · Jul 10, 2017 at 06:27 PM
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.
https://docs.unity3d.com/Manual/Tags.html https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
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.
Thank you for your answer. I am still very new to this, any chance you could show me how i can use the "FindGameObjectsWithTag("key")" in my code?
@chainylol I have update the answer for you. Does that clarify what I mean?
Once again, thanks. However, wouldn't that make it so i have to collect all the keys, and then collide with the door to destroy it? thats not what i want. I want the door to get destroyed once i pick up the final key, but maybe i didn't understand your code quite right.
Something like this maybe?
if (GameObject.FindObjectsWithTag("key").length == 0) { Destroy(this.gameObject); } }
And by the way, isnt it OnCollisionEnter? Not OnColliderEnter. heeelp :/
I did not get it to work unfortunately. How would you write the code if you were in this situation?
Answer by chainylol · Jul 10, 2017 at 07:08 PM
@jinkata please elaborate on your first solution with FindGameObjectsWithTag, im still very new to this. Thanks!
You can also use a list system. See the code below:
$$anonymous$$eyCode
// In the inspector drop and drag the linked door object here
public Transform linkedDoor;
doorScript ds;
void Start () {
ds = linkedDoor.GetComponent<DoorScript> ();
ds.Add$$anonymous$$ey (transform);
}
void OnCollisionEnter (Collision other) {
// Remove the $$anonymous$$ey
ds.Remove$$anonymous$$ey (transform);
// Destroy this key
Destroy (this,gameObject)
}
DoorScript Code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : $$anonymous$$onoBehaviour {
List<Transforms> keyList;
void Start () {
keyList = new $$anonymous$$eyList ();
}
public void Add$$anonymous$$ey (Transform key) {
keyList.add (key);
}
public void Remove$$anonymous$$ey (Transform key) {
keyList.Remove (key);
if (keylist.Count == 0) {
Destroy (this.gameObject);
}
}
}
Hey man, thanks for taking your time to answer. The code looks great to me, but i get these two errrors when pasting in the $$anonymous$$eyCode script: https://gyazo.com/421750a0d00138b0daa47370d0268ede
DoorScript seems fine though.
@Cornelis-de-Jager well actually it is just one error. accidentally made two scripts, sorry :D
Answer by Cornelis-de-Jager · Jul 11, 2017 at 12:35 AM
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);
}
}
}
Thanks for taking your time with this. I tried the code and i do get 1 error:
Assets/$$anonymous$$eyCode.cs(8,5): error CS0246: The type or namespace name `doorScript' could not be found. Are you missing an assembly reference?
Name of scripts: $$anonymous$$eyCode, DoorScript
In the $$anonymous$$eyCode script find:
public class $$anonymous$$ey : $$anonymous$$onoBehaviour
and replace it with:
public class $$anonymous$$eyCode : $$anonymous$$onoBehaviour
@Cornelis-de-Jager Did not solve it. Same error remains.
Does it work fine for you?