Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MortonPrice · Jul 10, 2019 at 03:46 PM · uibuttonstagstextmeshsetactive

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

 

  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MortonPrice · Jul 10, 2019 at 05:39 PM 0
Share

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.

avatar image Dawdlebird MortonPrice · Jul 10, 2019 at 07:12 PM 0
Share
 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";
         }
     }
 
 }
 
avatar image Dawdlebird Dawdlebird · Jul 10, 2019 at 07:14 PM 0
Share

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.

Show more comments
avatar image MortonPrice · Jul 11, 2019 at 12:44 PM 0
Share

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.

avatar image Dawdlebird MortonPrice · Jul 11, 2019 at 05:23 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

183 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges