Instantiating And Destroying Game Objects In Unity

Corey Daniely
2 min readMay 29, 2023

--

#Coding #Programmer #LearnToCode #GameDevelopment #GameDev #UnityDeveloper #SoftwareEngineer

When it comes to collision in Unity there are two forms. Selecting the enemy and the laser prefabs you can see inside the inspector they both have box colliders. These colliders are what detect collisions in the physical world. However game objects still cant collide unless we activate the physical system in Unity.In Unity we use physics through a system called the Rigidbody. The Rigidbody is what allows us to add physical properties to a game object.

There are hard surface collisions and then there’s trigger systems. an example of a hard surface collision would be something like a wine glass falling and then crashing on the floor. An example of trigger collision would be something like Mega Man getting a upgrade to his arm cannon it gives the illusion that you collected the power up.

The way this is all possible is through a setting inside the Box Collider called “Is Trigger”.

The next part which is important, Is determining who gets the Rigidbody. It’s okay for 2 to 3 game objects to have a Rigidbody but if there are more it stand a chance of being performance intensive if too many of your game objects are Rigidbody.

Looking at how collisions work in Unity there are two forms hard collisons and pass through collisions. There are also helper methods in Unity that are apart of the “MonoBehavior”. Within the “MonoBehavior” there are messages like OnDestroy, OnCollisionStay, OnMouseDown etc. The messages in “MonoBehavior” are called upon as collisions happen.

Inside the enemy script you can detect collisions. Inside the Debug.Log() other gives the object, transform gives the root of the object and Name gives the name of the object. With a successful collision detection set up and using these game methods I now can use logic to destroy game objects.

--

--

Corey Daniely
Corey Daniely

Written by Corey Daniely

0 Followers

Unity Developer

No responses yet