- Home /
How can I make the Player be destroyed when I touch the end of the camera?
hi guys i want my player to be destroyed when i hit the camera ... i used the following scrpits but it did not work
first on the camera I put a collider
and I entered this code
private void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("Player"))
{
Destroy (player);
}
}
but it did not work
so I inserted it into the player
private void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("MainCamera"))
{
Destroy (gameObject);
}
}
and still nothing
please help me!
First, you have a collider on your player object and the camera right? Is it 2D collider or 3D?
Second, check whether they are triggers. You can set them by ticking the trigger button on the collider.
Third, are the tags set properly?
Answer by rufopufo · Jul 15, 2019 at 11:07 AM
Hi there,
There is not a lot of information in your question, but i will suppose that your game is 2D, and that the "IsTrigger" component of the Camera's collider is set to true.
With these statements, the most probable mistake is that you don't have a Tag named "Player". My guess is that you are trying to compare it to the GameObjects name, instead of Tag.
So you have two options here, create a Tag, and set the Player's tag.
The second option is to compare directly the gameobjects name:
other.gameObject.name.Equals("Player")
Hope it helps, if this is not the case, we will need more information about your collider, names, tags, etc...
Your answer
Follow this Question
Related Questions
Unity crashes after I destroy a gameobject 1 Answer
Distance destroy object 3 Answers
Explosion triggers another explosion 2 Answers
how to destroy camera instatiated from prefab? 0 Answers
Active camera on Start 1 Answer