I have a canvas, a button and then textmeshpro text on that button. The button and text are children of the canvas. The script is attached to the canvas.
I am also reading the text in from a .txt file. In this way I can update button text by just editing the .txt file. The button text is defined in the .txt file like so
<O1,1,2,3>Button text here</O1,1,2,3>
And that indicates
O1 - button 1
1 - chapter 1
2 - branch 2
3 - phase 3
All of this works great. I can just edit the text file and the appropriate button shows the correct text and it handles as many buttons as I like.
What isn’t working, and what I need help with please, is finding a way to set the button and text to inactive when there is specific text present in the .txt file. In this case I have the script set up to inactivate the button and text (so hide/remove them both from the scene entirely) when the text is like
<O1,1,2,3>NADA</O1,1,2,3>
Sadly, that isn’t working. I am getting back the text from the .txt file but the button and text still display. Can anyone have a quick look and tell me what I am doing wrong?
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class CanvasController1 : MonoBehaviour
{
public string fileName = "example";
public StoryTextDisplay storyTextDisplay;
public TextMeshProUGUI textMesh;
public Button button;
public Canvas canvas;
void Start()
{
LoadAndDisplayText();
}
void LoadAndDisplayText()
{
if (storyTextDisplay != null)
{
int chapter = storyTextDisplay.chapter;
int branch = storyTextDisplay.branch;
int phase = storyTextDisplay.phase;
TextAsset textAsset = Resources.Load<TextAsset>(fileName);
if (textAsset != null)
{
string[] lines = textAsset.text.Split('\n');
foreach (string line in lines)
{
if (line.StartsWith($"<O1,{chapter},{branch},{phase}>"))
{
string text = line.Replace($"<O1,{chapter},{branch},{phase}>", "").Replace($"</O1,{chapter},{branch},{phase}>", "");
textMesh.text = text;
Debug.Log($"Reading text: {text}");
break;
}
}
}
else
{
Debug.LogError($"Text file not found: {fileName}");
}
}
else
{
Debug.LogError("StoryTextDisplay script reference not set!");
}
// I think this is the problem
if (textMesh.text == "NADA")
{
button.gameObject.SetActive(false);
textMesh.gameObject.SetActive(false);
}
else
{
button.gameObject.SetActive(true);
textMesh.gameObject.SetActive(true);
}
}
}