Activating an object clears the attached script components private variables?

Hi,

I’ve made a dialog window UI within a scene, and I’ve attached a script component to the top level gameObject in that particular hieararhy, which sets the dialog’s text, title, button texts, etc., and responds to the buttons’ OnClick() events. There’s a public function in that script named Open() which accepts various parameters of the dialog, such as those texts, sets the UI objects’ properties, and then calls gameObject.SetActive(true). (the gameObject is inactive by default to hide it until needed). I’ve created a prefab from the dialog window to make it easier to edit.

All that works fine, the dialog gets shown, the UI holds the right texts, OnClick() events are handled, EXCEPT, that I’ve noticed that the private variables of that script are set to null in the OnClick() handlers, it’s like activating the object clears them, or re-instantiates the script?

Why would that be? The Open() method in this script is called from another script which holds the reference to the top-level dialog gameObject which is established in the editor, so it should be the same instance of both the gameObject and the script, right?

Activating or deactivating an object should not have any effect on its internal script states. If they are getting reset, you may be doing this manually / accidentally, or it could happen through an exception. Are there any errors in your console?
Also, you said the object was saved as a prefab, so even if you somehow destroyed it and instantiated a new version, the default state of the script should, according to you, not contain nulls.
Other than that, it would probably help to see the code. If you do post code, please use code tags.

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"); });
    }

}

Hi there,

I’m currently make a third person multiplayer game in unity, and i’m kind of struggling to get nice third person movement
and i have tried everything but just didn’t fell right, or work right. So if anyone could maybe help me with some idea’s of what i could do or check out. And also this doesn’t really feel right for me only because i’m also trying to and in some animations.

And sorry, that i’m kind of off topic.

Create your own thread for this.

Thank’s,

but what do you mean

@ivoras your code should work. Try logging when you assign buttonOkHandler and buttonCancelHandler. Might it be that something else is calling Open()?

By that i mean i don’t really know what threads are so ya

A thread is a forum topic… a chain of replies dedicated to some question or content the first poster wrote. This thread is about figuring out what’s wrong with OPs code or structure to help him fix his problem as described in the title and his first post. Since your topic is not related, you should thus make your own thread and have people respond to that.

Hello

Okay thank you to Yoreki

Right, so I found out what happened: the OnClick() handlers set in the editor were referencing a function from the prefab in the file system, instead of in the object hierarchy which is the one which had the variables set.

1 Like