- Home /
how to destroy a separate object on touch with another object
I am completely new to scripting and i am creating a very basic game. You have to find 4 cubes (no rigidbody) and when you collide with one its supposed to destroy one of the planes blocking the final area. I created this script but it will not work. Here it is. using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void OnCollisionEnter(barrierdestroyer1) { Destroy(barrier1); } }
Can anyone help. Thanks.
Answer by HHammerite · May 09, 2018 at 09:14 PM
Hi! This is a simple problem that I sometimes encounter. For the OnCollisionEnter method to be called you will need a Rigidbody attached to the GameObject along with a Collider that is NOT set to IsTrigger. I also see that you are missing something in your code above.
The syntax for OnCollisionEnter requires a Collision parameter. This is passed automatically when a collision occurs. The Collison class provides you with some useful data which can help determine what you have hit. I'm assuming you want to know if the object it's colliding with has the name "barrierdestroyer1"? The code below will do a check to see if thats the case, if true then destroys the 'barrier1' gameObject.
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "barrierdestroyer1")
{
Destroy(barrier1);
}
}
OnCollisionEnter script reference
Hope this helps!
Answer by jonathangreenberg · May 10, 2018 at 01:00 AM
@HHammerite Thank you so much but i put the rigid body on the barrier and made sure its not trigger. But when i placed it on either object it said to fix compiler errors even though i think it should be fine. Here is the script
using UnityEngine;
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "barrierdestroyer1")
{
Destroy(barrier1);
}
}
Do you know what i did wrong. Thanks.
I read it and i just realized there are none it says it cant find the "script class" but i do not know what that means.
using UnityEngine; public class $$anonymous$$yExampleClass : $$anonymous$$onoBehaviour { void OnCollisionEnter(Collision collision) { if(collision.gameObject.name == "barrierdestroyer1") { Destroy(barrier1); } } }
The method 'OnCollisionEnter' must be in a class. In Unity this has to be the same as the file name without spaces. So of the file is called '$$anonymous$$yExampleClass.cs' then the class name should match that exactly.
Answer by jonathangreenberg · May 10, 2018 at 07:17 PM
@HHammerite I am sorry but i did that and now it is saying that "barrier1 does not exist in the current context". This does not make sense to me because i did everything right and barrier1 does exist in the scene. This has never happened to me before. Can you help? Here is the code just in case i messed up.
using UnityEngine; public class MyExampleClass
: MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{ if(collision.gameObject.name == "barrierdestroyer1")
{ Destroy(barrier1);
}
}
}
It might exist in the scene, but it doesn't exist in the current context, the current context in this case is the script itself. Since there's no variable in the script that defines what barrier1 is, it is undefined in this context.
You have to somehow get a reference to the barrier1 object. It's not clear what exactly barrier1 is here, is that the gameobject that has the script on it, or the game object that is colliding with the object that has this script on it?
If barrier1 is the gameobject that has this script on it, you can simply write
Destroy(gameObject);
ins$$anonymous$$d of
Destroy(barrier1);
which would destroy the current gameobject. If it is the gameobject that the gameobject that has this script on it is colliding with it, you get the reference passed with the OnCollisionEnter(Collision collision), as the variable collision. In that case, try writing
Destroy(collision.gameObject);
What is supposed to happen is when the player hits the barrier destroyer the barrier gets destroyed.
Answer by jonathangreenberg · May 10, 2018 at 08:13 PM
@shadowpuppet @Happeloy @HHammerite I am very sorry but i think i worded my question poorly. What is supposed to happen is that when the player (cube) touches the barrier destroyer than barrier 1 will be destroyed.
Answer by shadowpuppet · May 10, 2018 at 09:38 PM
so the player hits object 1 ( barrier destroyer) and then object 2 gets destroyed?If that is correct then I still don't know if this is a global thing or a specific thing. meaning if player hits a particular barrier destroyer then a particular barrier is destroyed? The script below is a script you could use on multiple barrier destroyers ( if you want) and just have a public variable for the barrier to destroy. the barrier destroyer is a trigger, the player has a collider an rigid body. This script would go on any barrier destroyer
public GameObject barrierToDestroy;// this would be barrier1 if the script is on barrier destroyer 1
void OnTriggerEnter(Collider other) {
if(other.CompareTag ("Player"))// tag your player cube as "Player"
{
Destroy(barrierToDestroy);
}
}
}
@shadowpuppet Why does it say "unexpected symbol game object"? Here is the code just in case i messed it up but i don't think i did.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroy : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
public GameObject barrier1;// this would be barrier1 if the script is on barrier destroyer 1
void OnTriggerEnter(Collider other) {
if(other.CompareTag ("Player"))// tag your player cube as "Player"
{
Destroy(barrier1);
}
}
}
You need to declare the GameObject before Start(). It's just kind of sitting there in your script. need to drag the barrier into the inspector of this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroy : $$anonymous$$onoBehaviour {
public GameObject barrier1;// this would be barrier1 if the script is on barrier destroyer 1
void Start () {
}
void Update () {
}
void OnTriggerEnter(Collider other) {
if(other.CompareTag ("Player"))// tag your player cube as "Player"
{
Destroy(barrier1);
}
}
}
@shadowpuppet I did what you said and for some reason when the player touches barrier destroyer 1 the plane does not even lose its collision. Is it because i am using play mode?