Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Activating an object clears the attached script components private variables?

Discussion in 'Scripting' started by ivoras, Jul 6, 2020.

  1. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    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?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    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.
     
  3. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    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:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7. using TMPro;
    8.  
    9. public class DialogWindowController : MonoBehaviour
    10. {
    11.  
    12.     public TMP_Text Title;
    13.     public TMP_Text Message;
    14.     public Button ButtonOK;
    15.     public Button ButtonCancel;
    16.  
    17.     private Action buttonOkHandler;
    18.     private Action buttonCancelHandler;
    19.  
    20.     public void Open(string title, string message, string buttonOKText, Action buttonOkHandler, string buttonCancelText = "", Action buttonCancelHandler = null) {
    21.         Title.text = title;
    22.         Message.text = message;
    23.         foreach (var c in ButtonOK.GetComponentsInChildren<TMP_Text>()) {
    24.             c.text = buttonOKText;
    25.         }
    26.         ButtonCancel.gameObject.SetActive(buttonCancelText != "");
    27.         foreach (var c in ButtonCancel.GetComponentsInChildren<TMP_Text>()) {
    28.             c.text = buttonCancelText;
    29.         }
    30.         this.buttonOkHandler = buttonOkHandler;
    31.         this.buttonCancelHandler = buttonCancelHandler;
    32.         gameObject.SetActive(true);
    33.     }
    34.  
    35.     public void Close() {
    36.         gameObject.SetActive(false);
    37.     }
    38.  
    39.     public void onButtonOkClicked() {
    40.         Debug.Log("onButtonOkClicked");
    41.         Debug.Log("#######################################");
    42.         Debug.Log(this.buttonOkHandler); // XXX: This gets set to NULL!
    43.         if (buttonOkHandler != null) {
    44.             Debug.Log("Going to buttonOkHandler()");
    45.             buttonOkHandler();
    46.         }
    47.         Close();
    48.     }
    49.  
    50.     public void onButtonCancelClicked() {
    51.         Debug.Log("onButtonCancelClicked");
    52.         if (buttonCancelHandler != null) {
    53.             buttonCancelHandler();
    54.         }
    55.         Close();
    56.     }
    57. }
    58.  
    And the code which calls Open():


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using EquinoxEngine.Core.Netcode;
    5. using EquinoxEngine.Core.Netcode.Model;
    6. using EquinoxEngine.Core.Scriptables;
    7.  
    8. public class BackgroundStuff : MonoBehaviour
    9. {
    10.  
    11.     public DialogWindowController DialogWindowController;
    12.     public PlayerInfo playerInfo;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         RLog.Log(this.name, "Starting up");
    18.  
    19.         StartCoroutine(SanityCheck());
    20.  
    21.     }
    22.  
    23.     public IEnumerator SanityCheck()
    24.     {
    25.         yield return new WaitForSeconds(5);
    26.         DialogWindowController.Open("Some window", "Some message.", "Ok", () => { Debug.Log("The user clicked OK"); }, "Cancel", () => { Debug.Log("The user clicked Cancel"); });
    27.     }
    28.  
    29. }
    30.  
     
  4. bash_the_coder

    bash_the_coder

    Joined:
    May 31, 2020
    Posts:
    15
    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.
     
  5. bash_the_coder

    bash_the_coder

    Joined:
    May 31, 2020
    Posts:
    15
    And sorry, that i'm kind of off topic.
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Create your own thread for this.
     
  7. bash_the_coder

    bash_the_coder

    Joined:
    May 31, 2020
    Posts:
    15
    Thank's,

    but what do you mean
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,342
    @ivoras your code should work. Try logging when you assign buttonOkHandler and buttonCancelHandler. Might it be that something else is calling Open()?
     
  9. bash_the_coder

    bash_the_coder

    Joined:
    May 31, 2020
    Posts:
    15
    By that i mean i don't really know what threads are so ya
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    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.
     
  11. bash_the_coder

    bash_the_coder

    Joined:
    May 31, 2020
    Posts:
    15
  12. bash_the_coder

    bash_the_coder

    Joined:
    May 31, 2020
    Posts:
    15
    Okay thank you to Yoreki
     
  13. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    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.
     
    Last edited: Jul 31, 2020
    Baste likes this.