0

I am making a project with Unity that using RestSharp API's.

I can specify and retrieve language keys and values ​​from the server without any problems, and I can keep these values ​​up to date in a dictionary and listen. As soon as the values ​​arrive, that is, when the dictionary is updated, when I want to send the data to the texts, only the active ones in the hierarchy are updated.

public void SetTexts()
{
    var languageTexts = FindObjectsOfType<LanguageKeys>();
    foreach (LanguageKeys languageText in languageTexts)
    {
        languageText.SetTexts();
    }
}

When I send the values ​​with these codes, they are written instead of the values ​​without any problems.

It also happens if I want to rerun the SetText() function with the OnEnable function, but when the values ​​are coming from the server, I get an error because the key values ​​in the Dictionary are null.

Is there a function like UnityEvent, Action<> where TextMeshProUGUI text can listen to the values ​​from the dictionary and update itself when the values ​​change? How can I improve the code so that it is a more optimized system?

derHugo
  • 83,094
  • 9
  • 75
  • 115
mhmmtysil
  • 11
  • 3

1 Answers1

0

FindObjectsOfType by default only returns active and enabled instances!

In newer Unity versions (2020.3) there is the optional parameter bool includeInactive so in order to include also the inactive ones you want to use

var languageTexts = FindObjectsOfType<LanguageKeys>(true);
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Thank you so much. I thought of that too. I'm working on version 2020.3.24f1 I didn't know it supported this feature just – mhmmtysil Dec 27 '21 at 10:59