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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Having problem with script to enable Text in Canvas

Discussion in 'UGUI & TextMesh Pro' started by marciotf, Sep 3, 2016.

  1. marciotf

    marciotf

    Joined:
    Jul 15, 2016
    Posts:
    15
    Hi,
    i am running Unity 5.3.5f1, and I just did a simple function to enable a text in the UI Canvas as below:

    Code (CSharp):
    1.     public Text gameovertext;
    2.  
    3.     public void EnableGameOverTxt()
    4.     {
    5.             gameovertext.enabled = !gameovertext.enabled;
    6.     }
    What happens is, if I start the Scene with the text enabled and I use the function to disable it, it works, text goes away. But if the text is disabled and I use the function to enable it, it does not work, the text is never shown.

    Can you please help me on this, I dont know what Im doing wrong.

    Thx
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Something else is going on with your code: I did the following
    1. Created a UI Panel and attached the script below to it
    2. Created a Text Child of that Panel.
    3. Dragged the Text into the public Text variable of the Panel Script
    4. Created a UI Button.
    5. Created a New OnClick Event and set it to ToggleText in Panel Script
    Panel Script:
    Code (CSharp):
    1. public class EnableTester : MonoBehaviour
    2. {
    3.     public Text text;
    4.  
    5.     public void ToggleText()
    6.     {
    7.         text.enabled = !text.enabled;
    8.     }
    9. }
    When I clicked the button the text disappeared. When I clicked it again it reappared. Worked perfectly over and over.

    Is this script attached to the Text Object itself. I'm not sure if enable/disable on the text also disables all scripts attached to it.
     
    Last edited: Sep 3, 2016
  3. marciotf

    marciotf

    Joined:
    Jul 15, 2016
    Posts:
    15
    The script is not attached to the text, its attached to the Canvas that the text belongs to, and the Canvas is always enabled.

    What I noticed is that if the text is enabled when I load the Scene(press the Play button) in the Editor, the function works fine to both enable and disable it. But if the text is disabled when I load the Scene, I cannot use the function to enable it.
     
  4. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Sooo, instead of Disabling the GameObject (which disables every attached component), disable just the UI specific component, and leave your Script Enabled?
     
  5. marciotf

    marciotf

    Joined:
    Jul 15, 2016
    Posts:
    15
    Sorry I didn't understand your answer.

    My script is not attached to the Text that I am disabling. It is attached to the parent Canvas of the Text, and this Canvas is never disabled in my Scene.

    So the script is never disabled when I run the function.
     
  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    When you say If the Text is disabled when you Press play in the editor, how are you disabling it. I assume you unchecking the Enable box of the Text in the editor. Which checkBox are you clicking. There is one directly above the RectTransform, and there is another CheckBox down by the Text (script) haflway down the editor.

    The top box is the enable / disable for the entire GameObject which you would set/unset in your code with
    Code (CSharp):
    1. text.gameObject.SetActive(true);
    This is very differnent than the 2nd CheckBox by the Text Script.. which is what we are toggling with enable/disable.
    So if your unchecking the wrong box before play starts thats why it doesn't work. Because unchcking the top one is basically saying SetActive(false); and your never turning it back on.
     
    marciotf likes this.
  7. marciotf

    marciotf

    Joined:
    Jul 15, 2016
    Posts:
    15
    hi takatov,
    you're completely correct.
    thx very much for pointing out my error, Im learning unity(and programming actually) by myself and sometimes its hard to learn the details. I had watched countless videos and tutorials and couldn't understand the issue, still it was so basic.
    now its working fine.

    Regards and thx again.