This is the relevant code
targetText = thisUpgradeCanvas.transform.Find("TargetText").GetComponent<TextMeshProUGUI>();
void SetTextForWhoToAttack(int value)
{
switch (value)
{
case 0:
targetText.text = "First"; // shows error
break;
case 1:
targetText.text = "Last"; // shows error
break;
case 2:
targetText.text = "Strong"; // shows error
break;
case 3:
targetText.text = "Weak"; // shows error
break;
case 4:
targetText.text = "Close"; // shows error
break;
}
}
This shows that on start the code is referencing the text as it should

This shows the error that it is null even though it shows it is not null

This is the entire class code
```csharp
**__using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CanvasController : MonoBehaviour
{
public GameObject upgradeCanvas;
GameObject thisUpgradeCanvas;
int attackIndex = 0;
HeroController heroController;
public TextMeshProUGUI targetText;
void Start()
{
heroController = gameObject.GetComponent<HeroController>();
thisUpgradeCanvas = Instantiate(upgradeCanvas);
targetText = thisUpgradeCanvas.transform.Find("TargetText").GetComponent<TextMeshProUGUI>();
thisUpgradeCanvas.SetActive(false);
}
public GameObject GetCanvas()
{
return thisUpgradeCanvas;
}
public void SetCanvasActive(bool value)
{
thisUpgradeCanvas.SetActive(value);
}
public void ChangeAttackIndexRight()
{
if (attackIndex < 4)
{
attackIndex++;
SetTextForWhoToAttack(attackIndex);
}
else
{
attackIndex = 0;
SetTextForWhoToAttack(attackIndex);
}
SetState(attackIndex);
}
public void ChangeAttackIndexLeft()
{
if (attackIndex > 0)
{
attackIndex--;
SetTextForWhoToAttack(attackIndex);
}
else
{
attackIndex = 4;
SetTextForWhoToAttack(attackIndex);
}
SetState(attackIndex);
}
void SetTextForWhoToAttack(int value)
{
switch (value)
{
case 0:
targetText.text = "First";
break;
case 1:
targetText.text = "Last";
break;
case 2:
targetText.text = "Strong";
break;
case 3:
targetText.text = "Weak";
break;
case 4:
targetText.text = "Close";
break;
}
}
void SetState(int value)
{
heroController.SetState(value);
}
}**
**```__**