Hello everyone!

So, yesterday, I wrote a script. It was wonderful. It worked.

Then I accidentally deleted a few simple prefabs and once they were put back together again, the scripts did not quite work together the way they were supposed to, even though I am pretty sure I had not changed a single character. I’ll try to explain as best as I can what I was doing but I am a beginner, so please bear with my terrible use of programming terminology.

In my scene I have two identical text boxes on top of each other. When the “main” text box is inactive, then the second one can be activated under specific circumstances (clicking on tagged objects). However, after the little accident, the second text box remains stubbornly active throughout!

So basically, I have a C# Monobehaviour “parent” script as well as another script which inherits from it.

In the former, I have:

public class TextBoxManager : MonoBehaviour {

	public GameObject textBox;
	public bool isStoryTextBoxActive;

	//...

	void Update() {

		if (textBox.activeInHierarchy == false) {
			isStoryTextBoxActive = false;
		} else if (textBox.activeInHierarchy == true) {
			isStoryTextBoxActive = true;
		}
	}
}

With the textBox correctly assigned to a Panel to which is attached a Text UI. (It is actually one of the prefabs that I accidentally deleted.)

In the second script, I have:

public class ClickObjects : TextBoxManager {

	void Update () {

		if (isStoryTextBoxActive == true) {
			textBox.SetActive(false); 
			// A note: I added this script after the problem manifested itself. Before that everything worked perfectly fine without it! I still left it in because it feels like added security, if you see what I mean.
		}

		if (isStoryTextBoxActive == false) {
			//...
		}
	}
}

With the textBox assigned to the SECOND panel with its own text UI.

After inspecting every element with care while in Play Mode, it seemed that one of the boolean (“isStoryTextBoxActive”) does not seem to update from one script to another anymore. At the beginning of the game, the empty game object that holds the TextManager script does register that the story text box is active, however its child empty game object with the ClickObjects script does not!

Once again, everything worked fine before.

Any help would be greatly appreciated! Thank you very much for your attention.

Hey @Fahryem, I actually found another issue with this idea, silly of me to only realise it now.
Whilst the second textbox is inactive, its update won’t be called.

So forget what I’ve said before!

You should probably add a third seperate class that’s sitting around on another object in the scene.

With this code:

using UnityEngine;
using System.Collections;
 
public class TextBoxManager : MonoBehaviour
{
    public static TextBoxManager Instance;

    void Awake()
    {
        Instance = this;
    }

    public GameObject m_mainTextBox;
    public GameObject m_otherTextBox;

    // Made them private, since you only really have to call the methods.
    private bool m_showAnyTextbox =  true; // True by default.
    private bool m_showingMain = true; // True by default.

    // Initialize the textboxes just to be sure.
    void Start()
    {
        _updateTextBoxes();
    }

    // Alternates between Main and Other textbox.
    public void ToggleTextBoxes()
    {
        m_showingMain = !m_showingMain;
        _updateTextBoxes();
    }

    // Alternates between having any textbox shown or none.
    public void ToggleAll()
    {
        m_showAnyTextbox = !m_showAnyTextbox;
        _updateTextBoxes();
    }

    // Update call for the textboxes. (No need to keep calling this in update).
    private void _updateTextBoxes()
    {
        m_mainTextBox.SetActive( m_showingMain && m_showAnyTextbox );
        m_otherTextBox.SetActive( !m_showingMain && m_showAnyTextbox );
    }
}

And then, anywhere in your code you can call…:

        TextBoxManager.Instance.ToggleAll();
        
        // OR
        
       TextBoxManager.Instance.ToggleTextBoxes();

…to achieve what you want!

I hope this code helps you out better, sorry for not having tested the previous suggestions before, haha!
Best of luck!