Change the gamobject's material makes the game really slow

In my game at every tap the color of 3 different gameobject changes between vìblack and white. probably the script isn’t good cause it makes the game really slow may you help me?

 public Material materialb;
    public Material materialn;
    public bool colorblack;
    int[] mat;
    public static ColorChanging current;

    void Awake()
    {
        if (current == null)
            current = this;
        else
            return;
    }
    // Start is called before the first frame update
    void Start()
    {
        colorblack= true;
    }

    // Update is called once per frame
    void Update()
    {
        if (!BallMovement.current.gameOver && /*!PauseMenu.current.pause &&*/ MovimentoPalla.current.partito)
            CahangeColor();
    }

    void ChangeColor()
    {
        if (gameObject != null)
        {
            if ((Input.GetMouseButtonDown(0) || Input.touches.Any(x => x.phase == TouchPhase.Began)) && !colorblack)
            {
                GetComponent<Renderer>().material = materialn;
                colorblack= true;
            }
            else if ((Input.GetMouseButtonDown(0) || Input.touches.Any(x => x.phase == TouchPhase.Began)) && colorblack)
            {
                GetComponent<Renderer>().material = materialb;
                colorblack= false;

            }
        }
    }

Thank you

There are countless of improvements possible in that small piece of code. You produce unnecessary garbage, you also instantiate new materials every switch which you do not properly destroy and the list goes on.

First and foremost the most important thing: Replace .material with .sharedMaterial.

Next thing you should do is avoid using Linq. The fancy Linq method “Any” seems so small and easy but it actually creates an IEnumerable object which does iterate through all available touches. You should also restructure your if condition order. Keep in mind that && is short ciruiting. Since you have your “colorblack” condition at the last condition you have all the expensive conditions before. So each frame you will evaluate both if statements up to it’s last condition. Just by moving colorBlack to the front your other conditions will only be evaluated for one if statement. Though your whole keypress detection should only be done once per frame anyways.

If this script really is only used on 3 objects you could do something like this:

Renderer rend;
void Start()
{
    rend = GetComponent<Renderer>();
    ChangeColor(true);
}

void Update()
{
    if (BallMovement.current.gameOver || !MovimentoPalla.current.partito)
        return;
    bool change = Input.GetMouseButtonDown(0);
    for (int i = 0; i < Input.touchCount; i++)
    {
        if (change || Input.GetTouch(i).phase == TouchPhase.Began)
        {
            change = true;
            break;
        }
    }
    if (change)
    {
        ChangeColor(!colorblack);
    }
}

public void ChangeColor(bool aNewColor)
{
    colorblack = aNewColor;
    if (colorblack)
        rend.sharedMaterial = materialb;
    else 
        rend.sharedMaterial = materialn;
}

As I said the most important change is the usage of sharedMaterial over material. The second most important thing is avoid Linq methods like Any. Third, cache GetComponent calls if they don’t change over the lifetime of the script. I also rearranged the code slightly. When you create a method and give that method a name, make sure the name really describes what the method does. Your ChangeColor method does not just change the color. It actually performs various input detection and under certain conditions it will change the color. I’ve seperated the input detection from the color changing. Now you can even call ChangeColor from another script and set that object’s color through that method.

I have no idea what you need the “current” (singleton like) static varible for when this script is attached to more than one object. Also what is the int array “mat” good for?