My bullet object has a Rigidbody 2D with Continuous collision detention. So in order to fully understand how to Destroy a GameObject in Unity, you also need to understand how to Instantiate an object.Create a new script (or use one already in progress) and add a new public GameObject variable, as above. Unity is the ultimate game development platform. To provide a better website experience, owlcation.com uses cookies (and other similar technologies) and may collect, process, and share personal data. It was driving me crazy, like "Why does it work for all these people and not for me ? Joined: Mar 22, 2013 Posts: 23. i have the cube moving but using 2D C# code i cant get the cube to destroy when it crashes into the box.i know there is probably an easy way to do this! We love to create and we love to share what we do! I am trying to get a bullet object destroy itself on collision, in a 2D game. Thank you whoever you are!Let me list for you some detections we would like to handle: for a large definition about collision detections in video games.So the sprite is the drawing, the collider is the shape, what does it mean?In this way the Gameobject will change according to the collider!How can we add a Collider 2D or Rigidbody 2D to a Gameobject?Have you noticed there is more than one Collider 2D to choose from? Unity has an explanation for each one, we just need to enrich it with some helpful examples.Let’s line up all the suspects then and have a look at each one of them (On the left is the sprite, on the right its collider)Letâs try to find out the version of the story of each Rigidbody 2D. If we want a normal object affected by physics, this is our body!If we want to move something not influenced by forces and gravity, remember itâs nature of interaction only with dynamic body types, this is our body!Hey partner! This just destroys the gameobject who has script on it.
The one process ties into the other, so we're going to approach this topic from the opposite end and look at Destroying a GameObject first.Destroying a GameObject in Unity requires, at its most basic, only two elements:Assuming you have those two elements in play in a script you can destroy a GameObject. Also I tried them. You can also use the Vector3 of another object to Instantiate your clone in a movable spot by creating another variable and assigning it to an active GameObject like so:This code grabs the x and y coordinates of the new GameObject, plugs them into the Instantiation code, and creates a clone at those same coordinates. 1- Select a Gameobject in the scene and click on add component.2- Type “collider 2D” or “rigidbody 2D” in the search box and select the component (for Collider 2D we will see different types, we’re just going to talk about it).Just enabling the right checkbox property in the Collider 2D component.The ball in this example has a dynamic Rigidbody 2D and it can be moved by applying forces and it’s affected by gravity!Let’s add a box with static Rigidbody2D, it will be an obstacle to overcome!What if we add a kinematic Rigidbody 2D instead of the static one to the box? If my player collides with any this rocks, I want to destroy rock clones (Spawned by RockSpawner) or set all rock clones made in game play mode to false. Hi there, I am trying to destroy just 1 tile at a time on collision in a 2D TileMap. Destroy Different Gameobject On Collision. e.g; the OnCollisionEnter method, you get a Collision. Using the guyGameObject variable you can create an Instance of the prefab using this line of code:When this script is run, assuming guyGameObject is assigned to a prefab, a clone of that GameObject will be created in your game, appearing at the same coordinates contained within the prefab. I can't find a way to make my projectile gameobject collide with my ofscreen collider. I have made a 2D game unity, and I have "RockSpawner" gameobject which spawns rocks when game starts. In this way we can control its movement without the influence of gravity. Then go back to the Inspector and add a prefab to your script, like so:You now have a connection to the prefab blueprint in your script. I am using the 2017.2beta and I can debug the collision with the right coordinates, but I am not able to just destroy that 1 tile on contact.