How to change the color of a box using only functions

Pretty cool stuff here. Let’s first start by getting a cube in our scene.

Next, let’s create a function that will change the color of the cube. Thinking about this logically, we need to do two things — get the cube and get the color. Those are 2 parameters we need. So let’s add those 2 parameters.

Next, let’s create a if statement that checks if the space bar has been pressed. Once it has, let’s pass in the parameters we want the function to run. What is great with this is I have a Function that simply takes an object and changes it’s color. But I can create the input to decide what I want it to do. So in this case, I can tell the input to grab the cube and change it blue or drop in another object and make it turn green. This is where I assign what I want to do. The function is simply the machine that does it.

Finally, I tell the function to take in the object and color parameter and apply it.

Unity Developer and Software Engineer who loves making games with GameDevHQ