C# Challenge - Color Change at Collision

Damn! Collided with something! Better change the color so it wasn’t me…

Timo Schmid
.Net Programming
3 min readJun 3, 2021

--

Photo by David Pisnoy on Unsplash

Maybe, at some point of your game, you want to change the color of an object as soon as it collided with another object. Let it be to disguise the player or to signalize that something new can now happen.

Something like that is the task for today. We simply shall change the color after hitting (and colliding) with a wall:

Today’s task!

I. Creating the variables we need
We start as always: Breaking down the task and declaring variables afterwards.

  • We need control player movement. As we only need to move left and right, we just need a variable for controlling the horizontal input
  • Movement won’t work without some movement speed, so we need a variable for that as well
  • The color should be changed when a collision gets detected. To achieve that, we need a reference to the MeshRenderer on the player

However, this time we do not need any global variables at all. We can create all variables we need with local variables.

Local variables are only accessible inside the method you create them in.
Global variables can be accessed from anywhere.

II. Move the player!
Let’s start with the movement method.

We need:

  • …the horizontal input value, which will be grabbed via Input.GetAxis
  • …a variable to store the movement speed in
  • …To let the player move based on the Input value and speed

In code, this will look like this:

The Movement() method

You might have noticed the const modifier in front of the speed variable. Because this variable will never change (and cannot be changed because it is a not serialized, private variable), its value stays constant. Because of that, we can use the const modifier without creating any issues!

III. Detecting the trigger of a collision
Now it’s time to check for any collision.

  • To makle things easier, let’s create a Tag named “Wall” and compare the tag at collision.
  • If the tag is “Wall”, we want to access the MeshRenderer and change its color to a random one.

In code, this will look like this:

The color changing logic!

Unity uses float values for the RGB values of the colors. That’s why the range is between 0f and 1f.

IV. Finishing up
Last thing to do is, you guessed it, calling the method inside void Update(). Update() because movement can happen at any time and also takes longer as a single frame. Lastly, attach the script onto the player.

The finished end result will look like this. The GIF will show the process four times, so you can make sure the color really is a random one:

Working color change!

For the case it’s not working for you:

  • Check if your Player and Wall both have a Collider (NOT a Collider2D!) and “Is Trigger” is ticked
  • Check if the Player has a Rigidbody (NOT Rigidbody2D!) component

And that’s it for todays article!
I hope I could help you out with this little article here.
Take care and until next time!

--

--

Timo Schmid
.Net Programming

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)