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

Question My setting menu wont show up using SetActive

Discussion in 'Scripting' started by roblenoa000, Mar 3, 2023.

  1. roblenoa000

    roblenoa000

    Joined:
    Sep 12, 2022
    Posts:
    5
    Im trying to have a settings menu using TMP and when I try to use SetActive(true) it doesn't work, I've tried changing the button but it wont work this is the entire part of the script I'm using. What am I doing Wrong?

    Code (CSharp):
    1. [SerializeField] GameObject settings;
    2.  
    3. private bool active = false;
    4. void Update()
    5. {
    6. if (Input.GetKeyDown(KeyCode.Tab))
    7.   {
    8.       if (!active)
    9.       {
    10.           settings.SetActive(false);
    11.  
    12.       }
    13.       else if (active)
    14.       {
    15.           settings.SetActive(true);
    16.  
    17.       }
    18.   }
    19. }
     
    Last edited: Mar 3, 2023
  2. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    It looks like you were a bit too excited when you started and accidentially typed void with a capital V
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    Are you actually setting the value of the variable
    active
    anywhere?
    It's not in that bit of code, so if you don't set that variable to true it will never reach the
    SetActive(true);
    line.
     
  4. roblenoa000

    roblenoa000

    Joined:
    Sep 12, 2022
    Posts:
    5
    No I accidentally capitalized it putting it in the forum
     
  5. roblenoa000

    roblenoa000

    Joined:
    Sep 12, 2022
    Posts:
    5
    i left it out but it is in there and its set as false at start
     
  6. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    You also accidentially forgot to update the active variable after toggling the visibility of the settings menu, which is why it's not working properly. ;)

    Check this:
    Code (CSharp):
    1. if (!active)
    2.       {
    3.           settings.SetActive(false);
    4.           active=true;
    5.       }
    6.       else if (active)
    7.       {
    8.           settings.SetActive(true);
    9.           active=false;
    10.       }
     
  7. roblenoa000

    roblenoa000

    Joined:
    Sep 12, 2022
    Posts:
    5
    I figured it out, i was never setting active to true or false out side of the start
     
  8. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    So I get no credit for pointing it out earlier? :(
     
    Kurt-Dekker likes this.
  9. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    Neither do I :(
     
    Kurt-Dekker likes this.