Unity Change Object Color When Touched

Niraj Karki
2 min readSep 10, 2023

--

Objective: Create a program that when you hit the space key, the sphere will fall. When the sphere hits the floor, it should turn the floor blue and the sphere should be destroyed.

Fill your scene with a floor and a sphere up in the air.\

Then extend the box collider of your floor to extend a litter above the surface. Now create and set the tag of the floor object as “Floor”.

Now create a player script and create a Rigidbody variable. Set reference for Rigidbody on start. When the player hits space key, enable gravity on the Rigidbody which will then make the object (player) fall.

Create a OnTriggerEnter function which will then detect the trigger when the object passes through an object with tag “Floor”. When the Player hits the floor, access the material component of the floor through mesh renderer and change floor color to blue. Then destroy the player object.

And this is the final result.

--

--