The code is only a part of it, I’ll try to be more clear:
Scene hierarchy cotains, among other things, a prefab which is the dialog window UI gameObject, and a script named BackgroundStuff. This BackgroundStuff script contains a reference (set in the editor) to the dialog gameObject. It creates a co-routine in Start() which waits 10 seconds and if a certain condition is met, calls the Open() method which belongs to another script named DialogWindowController which is attached as a component to the above mentioned dialog gameObject prefab. This Open() method sets UI elements to desired values and activates the dialog gameObject. All of that works fine. The UI elements (texts, buttons) show the correct strings before the object is activated. I don’t instantiate anything by myself.
The DialogWindowController implements OnClick methdods for the buttons. Those are referenced in buttons in the editor. The methods get called fine – EXCEPT they see private variables which should have been set by the Open() method as null.
But, if you’re saying that activating a gameObject doesn’t reset its private variables, then something’s wrong with my code flow.
Here’s the entire, very simple, DialogWindowController:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogWindowController : MonoBehaviour
{
public TMP_Text Title;
public TMP_Text Message;
public Button ButtonOK;
public Button ButtonCancel;
private Action buttonOkHandler;
private Action buttonCancelHandler;
public void Open(string title, string message, string buttonOKText, Action buttonOkHandler, string buttonCancelText = "", Action buttonCancelHandler = null) {
Title.text = title;
Message.text = message;
foreach (var c in ButtonOK.GetComponentsInChildren<TMP_Text>()) {
c.text = buttonOKText;
}
ButtonCancel.gameObject.SetActive(buttonCancelText != "");
foreach (var c in ButtonCancel.GetComponentsInChildren<TMP_Text>()) {
c.text = buttonCancelText;
}
this.buttonOkHandler = buttonOkHandler;
this.buttonCancelHandler = buttonCancelHandler;
gameObject.SetActive(true);
}
public void Close() {
gameObject.SetActive(false);
}
public void onButtonOkClicked() {
Debug.Log("onButtonOkClicked");
Debug.Log("#######################################");
Debug.Log(this.buttonOkHandler); // XXX: This gets set to NULL!
if (buttonOkHandler != null) {
Debug.Log("Going to buttonOkHandler()");
buttonOkHandler();
}
Close();
}
public void onButtonCancelClicked() {
Debug.Log("onButtonCancelClicked");
if (buttonCancelHandler != null) {
buttonCancelHandler();
}
Close();
}
}
And the code which calls Open():
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EquinoxEngine.Core.Netcode;
using EquinoxEngine.Core.Netcode.Model;
using EquinoxEngine.Core.Scriptables;
public class BackgroundStuff : MonoBehaviour
{
public DialogWindowController DialogWindowController;
public PlayerInfo playerInfo;
// Start is called before the first frame update
void Start()
{
RLog.Log(this.name, "Starting up");
StartCoroutine(SanityCheck());
}
public IEnumerator SanityCheck()
{
yield return new WaitForSeconds(5);
DialogWindowController.Open("Some window", "Some message.", "Ok", () => { Debug.Log("The user clicked OK"); }, "Cancel", () => { Debug.Log("The user clicked Cancel"); });
}
}