Color Change Using Button

2- Changing the Color of an Object Using a Button in Unity

This post is related to the previous post about changing the color of an object, so be sure to read it first because I explained something there that I will not explain here in order not to repeat it. Another point is to have the GitHub desktop software on your PC and download the project that I explain here from this link. The final result of the description here is saved in the scene file called ColorChangeWithButtonScript in Assets > Scenes, which you can use for practice.

In this post, as the title suggests, we are going to use a few lines of simple code to change the color of a mug by pressing a button in the scene and creating a color picker for determining the color, where we can choose the color before it.

If I want to say in order:

1- In the Assets tab, create a C# script and set a recognizable name for it. I chose setColorWithButton. Now open it in Visual Studio by double-clicking on the script

2- Add these two lines. The first line defines a public variable named myTestObject that we can connect the mug model to it. After “public”, I added the GameObject class to say that what is supposed to be connected to this variable in Unity is a GameObject and to be able to access some things related to the model that we can change later.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class setColorWithButton : MonoBehaviour
{
    public GameObject myTestObject;
    public Color myColor;

    public void myColorChanger()
    {
        var myRenderer = myTestObject.GetComponent<Renderer>();
        myRenderer.material.SetColor("_Color", myColor);
    }
}

In the next line, we defined a variable named myColor with public access so that we can see it inside Unity. What “Color” does is that it allows us to determine a desired color in Unity, which is called Color Constructor, and at the same time, it saves the color based on RGBA, where the first three letters are for red, green, and blue, and A is the first word for Alpha which determines the level of transparency.

3- Save here and go back to Unity, create a GameObject and attach the script to it. You can see that we have a part to connect the model and a part to determine the color

4- Connect the mug model to My Test Object and return to Visual Studio

5- Define a public method called myColorChanger. See here about the method and read the previous post about “void” which I explained with an example.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class setColorWithButton : MonoBehaviour
{
    public GameObject myTestObject;
    public Color myColor;

    public void myColorChanger()
    {
        var myRenderer = myTestObject.GetComponent<Renderer>();
        myRenderer.material.SetColor("_Color", myColor);
    }
}

6- Add the following two lines to the method, which are exactly the same last two lines that I used in the previous post, the explanation is also there.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class setColorWithButton : MonoBehaviour
{
    public GameObject myTestObject;
    public Color myColor;

    public void myColorChanger()
    {
        var myRenderer = myTestObject.GetComponent<Renderer>();
        myRenderer.material.SetColor("_Color", myColor);
    }
}

7- Save the file and return to Unity

8- Create a UI > Image that we want to use as a button. There is also a simple circular image in the project files that you can drag and drop in the Source Image section of the Inspector tab.

9- As the Image is selected, click on the Add Component button at the bottom of the Inspector tab and add a Button, and then click on the + sign in On Click.

10- Now take the GameObject from the Hierarchy tab and drop it in the “None (Object)” section until it is connected

11- Click on “No Function” and select the myColorChanger method from the setColorWithButton class according to the image

With this, whenever you click on the image of the button, the myColorChanger method is executed, which contains codes to change the color of the model that we attached to the GameObject, which is the mug model.

12- Select the GameObject from the Inspector tab and select a desired color through My Color and then press the play button. After running, by clicking on the button, the color of the mug should change to the selected color.

If you have any questions or comments, feel free to leave them in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *