Home c# Remove the Unity object

Remove the Unity object

Author

Date

Category

There is a sphere and a cube. How to make so that when a collision with a cube sphere was removed?

The cube hung such a script, but he removes the cube himself:

private void oncollisionenter (Collision Collision)
{
  Float Posx, Posy, Posz;
  POSX = transform.position.x;
  POSY = transform.position.y;
  Posz = transform.position.z;
  Destroy (GameObject);
}

Answer 1, Authority 100%

if (collision.gameobject.name == "sphere_name") {
  Destroy (Collision.GameObject);
}

or

if (collision.gameobject.compareteag ("sphere_tag") {// if (Collision.gameObject.tag == "Sphere_tag")
  Destroy (Collision.GameObject);
}

Answer 2

Check is not required. In your case, the code will be the following

private void oncollisionenter (Collision Collision)
{
  Float Posx, Posy, Posz;
  POSX = transform.position.x;
  POSY = transform.position.y;
  Posz = transform.position.z;
  Destroy (Collision.GameObject);
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions