Missing component exception when I adding component

I making chess & making spheres what shows where can walk selected figure & i need 2 delete it when selected other figure, but components doesn’t adds to spheres

Adding context:

public static void select(ChessVector vector)
{
    OnSelect(GetFigure(vector), EventArgs.Empty);

    selected = vector;
    ChessVector[] canBeat = GetFigure(vector).canBeat();
    GameObject[] moveSpheres = new GameObject[canBeat.Length];
    int counter = 0;

    foreach(ChessVector i in canBeat)
    {
        Vector2 coords = ChessVector.GetVectorFromCoordinates(i);
        bool isFigure = GetFigure(i) != null;
        float size = isFigure ? 0.7f : 0.3f;
        moveSpheres[counter] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        moveSpheres[counter].transform.position = new Vector3(coords.x, 1, coords.y);
        moveSpheres[counter].transform.localScale = new Vector3(size, size, size);
            moveSpheres[counter].AddComponent<OnClickForSpheres>();
        if (moveSpheres[counter].GetComponent<OnClickForSpheres>() == null)
            moveSpheres[counter].GetComponent<Renderer>().material = materials[1];
        OnSelect += moveSpheres[counter].GetComponent<OnClickForSpheres>().delete;
    }
}

Component code:

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

public class OnClickForSpheres : Component
{
    bool b = false;
    public void delete(object sender, EventArgs args)
    {
            Destroy(gameObject);
    }
}
  1. Your OnClickForSpheres class derives from UnityEngine.Component. Only scripts that derive from MonoBehaviour can be added to GameObjects.
    Hence, the OnClickForSpheres is never added to the sphere GameObjects.

  2. Additionally, you have a counter variable that is not being incremented in your for loop.

  3. Once you get step 1 above working, moveSpheres[counter].GetComponent<OnClickForSpheres>()
    will never be null, so your if statement will always evaluate to false, so moveSpheres[counter].GetComponent<Renderer>().material = materials[1];
    will never be executed.