- Home /
Hide or Show Text objects from ui.button
I'm trying to implement different languages. i have a canvas showing numerous buttons, almost 100 buttons. most of which are turned off, until another buttons turns them on. each button has 2x textmesh pro objects. 1 English, 1 French. in my options i want to click on my french button and it turns off all English text and turns on all French text. All English text is tagged "English" and the French is tagged as "French"
Using a script i can hide some of the English tagged objects, but only for game objects that are active. its does not hide text that is a child of a button that is currently hidden. So when i turn a new button on, the English text is visible but cant unhide (Set active True) the french tagged objects. so i just have buttons with no text.
Any ideas on how i can achieve this would be grateful. i would like to use tags as there is just so much text objects.
So its almost working, i just need to be able to hide / Setactive false objects that are child of game objects that are turned off. below is the code i'm using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowFrench : MonoBehaviour
{
public GameObject[] english;
public GameObject[] french;
void Start()
{
english = GameObject.FindGameObjectsWithTag("English");
english = GameObject.FindGameObjectsWithTag("French");
}
public void Disable()
{
foreach (GameObject e in english)
{
e.SetActive(false);
}
}
public void Enable()
{
foreach (GameObject f in french)
{
f.SetActive(true);
}
}
Answer by Dawdlebird · Jul 10, 2019 at 04:11 PM
Hm, are you sure having all these duplicate gameobjects is the way to go?
Either way: I would suggest a small script on each button that can hold either two strings or two gameobject references to your textobjects, which either set the text of a textmesh object or enables the appropriate text node. You can put a public function in there that handles this setting of the right text and have it called automaticly on OnEnable(); Then put a script on the root of your canvas that can search for all these scripts and call that public method in case you need to toggle languages at runtime.
And I would recommend using strings that set a single textmesh rather than toggle between two textmeshes. Less Gameobjects in your hierarchy. Also; if you have some localization class like that on your textobjects that simply sets the text you can later choose to have those refer to some localization text file via an index and can then easily add more languages.
Could you explain how I could write the script for the buttons and the script for the canvas. Yes I understand having all the duplicate game objects isn’t the best, I didn’t know how else to do it. Apologies I’m not the best with C# still learning.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using T$$anonymous$$Pro;
public class localizeButtonText : $$anonymous$$onoBehaviour
{
// static values are accessable from other scripts without having access to a component directely. A parameter like this should probably
// go into your game manager or playerprefs though. And you can use your language option to set this variable from anywhere
public static int languageIndex = 0;
public string englishText; // languageindex = 0
public string frenchText; // languageindex = 1
public T$$anonymous$$P_Text text;
private void Awake()
{
text = GetComponent<T$$anonymous$$P_Text>();
}
private void OnEnable()
{
SetText();
}
// public function you could call from a canvas root on all objects to set the appropriate text
public void SetText()
{
if (languageIndex == 0)
{
text.text = "englishText";
}
else if (languageIndex == 1)
{
text.text = "frenchText";
}
}
}
the canvas script would do a GetComponentsInChildren to fetch all these scripts and iterate throught hem when you swap language at runtime. If you put that in a public function you can hook it up to a UI button or whatever it is you use to toggle language.
aww im with you now. i have added the script to the first bit of text, and filled in what i want the English text to be and the french text. my empty text object then automatically fills in when i hit play, perfect!
Could you explain again, the other script i now need to add onto my french language button to toggle the from int 0 to 1. which i believe would remove all 0 English text and write 1 french text.
i'm very close now thank you.
unfortunately i am on my own with this, as we have no coders at work.
Are you familiar with static variables? They have the benefit of being shared by all members of the class (so they're allocated only once) and you can access it directly by the class, so you could have a toggle script that does "localizeButtonText.languageIndex = 1" for example. (I see that I forgot to name the class with a capital, it should have been "LocalizeButtonText")
It really doesn't matter from where you do this, but it should also call that script you put on the root of your canvas that holds a list of all localizeButtonText components so it can iterate over them calling their SetText() function. Since they will look at that static int you don't need to pass anything, they'll simply apply the right text.
Your answer
Follow this Question
Related Questions
UI not working after build 0 Answers
Menu not getting Keyboard Input 0 Answers
UI not responding. 3 Answers
Menu works in stand alone but not in Game view in Editor 0 Answers
Button gets stuck in Pressed state 1 Answer