[SOLVED] I'm getting a NullReferenceException when using image.color = Color.clear;

So heres my code:

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

public class TouchController : MonoBehaviour
{

    public TextMesh Check;
    public Graphic left;
    public Graphic right;
    Color rc;
    Color lc;

    public void Start()
    {
        left = GetComponent<Graphic>();
        right = GetComponent<Graphic>();
        rc = Color.clear;
        lc = Color.clear;
    }
    public void LeftClick()
    {
        Check.text = "left";
        left.color = lc;
        right.color = rc;
    }
    public void RightClick()
    {
        Check.text = "Right";
        left.color = lc;
        right.color = rc;
    }
}

Everytime RightClick() or LeftClick() is run i get a NullReferenceException on the line left.color = lc; anyone know why?

p.s I have set the graphics and textmesh in the inspector.

what is “Graphic” component, Should it be Image ?

I tried image as well, but i had no success

is it ui image object or sprite renderer or something else ?

its a ui image

public TextMesh Check;
    public Image left;
    public Image right;
    Color rc;
    Color lc;

    public void Start()
    {
        left = GetComponent<Image>();
        right = GetComponent<Image>();
        rc = Color.clear;
        lc = Color.clear;
    }
    public void LeftClick()
    {
        Check.text = "left";
        left.color = lc;
        right.color = rc;
    }
    public void RightClick()
    {
        Check.text = "Right";
        left.color = lc;
        right.color = rc;
    }

something like this, if the error stays then check if the script still have the link to the image at the click-time. Start the game, then the game will be paused by the getting error. Switch to the scene tab with running game and check Image fields.

Ok so, i did that and the error stayed, the game still ran despite the error and I found that when the game was being run the link to the Image was lost. Also there was no change with any of the image fields

oh, I think I did wrong with the script.

left = GetComponent();
right = GetComponent();
there is no need for both lines. You have already this link from the
public Image left;
public Image right;
And the line with getcomponent overwrites this link.

Just try to delete Lines 9 and 10

Wow, thanks so much. That worked perfectly, I didn’t realise that GetComponent would overwrite the link.