Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Awake/Start/OnDisable when object is disabled.

Discussion in 'Scripting' started by K1ng Zero, Feb 25, 2016.

  1. K1ng Zero

    K1ng Zero

    Joined:
    Aug 24, 2013
    Posts:
    4
    Hi. I've made a script which is responsible for languages in my game - It calls an event when I change language, so every subscribed game object is changing it's text, but the problem is that some UI elements in menu are disabled until I go to certain menu page and neither Start, Awake or even OnDisable gets called till I enable it, so it can't subscribe to this event. As soon as it subscribes to event everything is fine - every game object with text component is changing language when it's disabled or enabled.

    My question is - can i somehow subscribe to event when game object is disabled at start?

    This is the part of script that is attached to game objects with Text component:
    Code (CSharp):
    1.    [SerializeField]Text text;
    2.  
    3.    void Awake()
    4.    {
    5.       RegisterOnLanguageChanged();
    6.    }
    7.  
    8.    void ChangeText(Dictionary<string, string> _languageDictionary)
    9.    {
    10.       if (text == null)
    11.          text = this.GetComponent<Text>();
    12.       try
    13.       {
    14.          text.text = _languageDictionary[this.name];
    15.       }
    16.       catch
    17.       {
    18.          text.text = this.name;
    19.       }
    20.    }
    21.  
    22.    void RegisterOnLanguageChanged()
    23.    {
    24.       LanguagesScript.OnLanguageChanged += ChangeText;
    25.    }
    26.  
    27.    void UnregisterOnLanguageChanged()
    28.    {
    29.       LanguagesScript.OnLanguageChanged -= ChangeText;
    30.    }
    31.  
    32.    void OnDestroy()
    33.    {
    34.       UnregisterOnLanguageChanged();
    35.    }
    36.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can always start things enabled for a frame, then disable them.

    Another alternative is to simply poll the language in Awake.
     
    K1ng Zero likes this.
  3. K1ng Zero

    K1ng Zero

    Joined:
    Aug 24, 2013
    Posts:
    4
    Ok, I've made it with your 2nd method which I don't think is the best idea, but it works. I think there should be other way, but I'll keep that for now till I find other way. Anyway thank you for an answear:)
     
  4. hsuchc8888

    hsuchc8888

    Joined:
    Jan 15, 2016
    Posts:
    8
    Hi K1ng Zero, did you find any other way to do this?