Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to make visible/invisible different UI canvas ?

Discussion in 'Scripting' started by rgarand64, Dec 16, 2023.

  1. rgarand64

    rgarand64

    Joined:
    Feb 17, 2022
    Posts:
    15
    ONLY SOLUTION FOUND: multiple canvas, enable only one at a time:
    switch (level)// zero is first, one is second etc....
    {
    case 1:
    level1.SetActive(false);
    level2.SetActive(true);
    break;
    case 2:
    level2.SetActive(false);
    level3.SetActive(true);
    break;
    case 3:
    level3.SetActive(false);
    level4.SetActive(true);
    break;
    }
    Hi all,
    Have a multi level game, want the level menu to show right text.
    Have a menu with buttons, want to change only cue text on level.
    tried many variation of "FindGameObjectWithTag" but it only returns active ones, but I want only one text rendered at a time. If I use layers, will be limited to 23 levels ( 8 for editor + 1 for Light) 32-9, and it's not elegant.
    Maybe with render on/off ? How would you do it ? made many searches and lectures to no avail...stock there.
    My hyerachy:
    upload_2023-12-16_18-4-38.png
    Want to activate only one of the Text_Lx at a time...
    PS: was easy to change Header text, only a digit, but others are many lines long...

    TIA,
    regards Robert
    2021.3, VR, XR,
     
    Last edited: Jan 7, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,410
    Make a public or SerializeField reference and drag the Canvas(es) in you want, operate on them.

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.