Sitemap

Unity Change Object Color When Touched

2 min readSep 10, 2023
Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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”.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

And this is the final result.

--

--

No responses yet