NullReferenceException: Object reference not set to an instance of an object

Hello,
I am pretty new to C# and I’m trying to make a game in unity. The game is a clone of pong. I’ve been doing very well until now, but when I try to change the score label text when it hits the goal, it gives me an error. Here is my goal script:

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

public class RightGoal : MonoBehaviour
{
    private int leftScore = 0;
    public GameObject canvas;
    void OnTriggerEnter2D(Collider2D collider)
    {
        leftScore += 1;
        GameObject leftScoreGameobject = canvas.transform.Find("LeftScore").gameObject;
        Text leftScoreText = leftScoreGameobject.GetComponent<Text>();
        string leftScoreStr = leftScore.ToString();
        Debug.Log(leftScoreStr);
        leftScoreText.text = "1";
        collider.transform.position = new Vector2(0f, 0f);
        collider.GetComponent<Ball>().Direction();
    }
}

And here is a screenshot of the whole unity editor(in case it helps):

The exact error is:
NullReferenceException: Object reference not set to an instance of an object
RightGoal.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/Scripts/Goals/RightGoal.cs:17)
I searched many forums, and they said that it may be something missing, but i don’t see anything that is not where it is supposed to be.
I hope someone can help me!
P.S: RightGoal.cs is the script above the image.
P.S. 2: I am happy to give more info if you need

When you use Find or GetComponent, there’s a chance that it fails to find the object, and returns null. It’s much easier and quicker for you to try to investigate what ended up being null. Also, the error tells you exactly what line the problem is on. The number at the end is the line number.

I tried Debug.Log after Find, after GetComponent and after changing the text and I found that it seemed like it couldn’t get the Text component(I don’t know why). Thank you, @RadRedPanda

But can you please tell me why is this happening?
Here is the image of the Text component of the LeftScore GameObject in the inspector:

That is a TextMeshPro component, not the original Text component from Unity. I’m pretty sure the component is called TextMeshProUGUI.

Thank you SOOOOOOOOO much!!! It worked!!!

Always the same answer, always the same three steps:

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that