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

How do i toggle UI button from within js

Discussion in 'UGUI & TextMesh Pro' started by JackSparrow1, Jun 21, 2015.

  1. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    Unity 4.6
    Hi, Im trying to toggle a UI buttons state from inactive to active when my code variable reaches a value within js.
    "I just need to know how to switch the ui buttons state,(i.e. from disabled to interactable) within js" im new to js and unity, so dont know the full mechanics of either yet. Anyone know how i can do this?
     
    Last edited: Jun 21, 2015
  2. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    If i hard code all the GUI then its not a problem, but thats not what im asking.
     
  3. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    Looking at unitys docs i see there are commands,functions, but the descriptions for use are very limited.
    Ive tried incorporating, but without being able to see full functioning examples im stuck.
    Heres where i am atm."getComponentInParent.(Mygamebutton).active=true "
    Needless to say its not accepting the argument. I know there is the command "gameObject.SetActive(true)"
    but how do i set the specific game UI component (buttonscript) of UI gameobject "mygamebutton"?
     
  4. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    ok, this is as far as i can get without help.
    "mygamebutton" is the UI button on my panel. "Button" is the UI button script that im trying to enable.

    GameObject.FindGameObjectWithTag("mygamebutton").gameObject.GetComponent(Button).enabled = true;
     
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,793
    Not sure but try getting the reference first and then enable. Often things will not wok for me all in one line so I get the reference as a var and then do what I need to to the reference.
    Code (JavaScript):
    1. var go : GameObject = GameObject.FindGameObjectWithTag("mygamebutton");
    2. var btn : UI.Button = go.GetComponent.<Button>();
    3. btn.enabled = true;
     
  6. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    Thanks ippdev :) Im getting the error (The name "Button" does not denote a valid type(not found)
    So i change line 2 to go.GetComponent.<Button>(); to go.GetComponent.<Unity.Engine.UI.Button();
    which compiles but on run removes all other GUI elements that are drawn in my main code, and not the specific Element, of the Unity panel, (UI Button, script element)

    Idk, maybe i need to have this on a seperate script to my main code and attatch that script to the gameobject itself then somehow get the variable passed from the main script to the gameobject secondary script to turn on the UI Button script. so the function becomes actve. Would that be "public var myvariable" to make the variable global accross all scripts?

    btw how did you get your code pasted here, when i try it it all comes out in jappaneese.
     
    Last edited: Jun 22, 2015
  7. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,793
    To get code hit the icon between the filmstrips and the floppy disc. Instead of activating the component you could just turn the actual game object off and on. This is what I do to avoid fiddly bits. Set a variable field in your script for it and drag and drop the pertinent control to it. then use...
    Code (JavaScript):
    1. var myButtonGO : GameObject;
    2. var myButton : UI.Button;
    3.  
    4. function TurnOnMyButton () {
    5.      myButtonGO.SetActive(true);
    6.      //then whatever button specific code you have
    7. }
    I prefer to call the function direct from the buttons that control such..it keeps everything tidy in code.
     
  8. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    Thanks again for your help and time ippdev :)

    I still had problems implementing it, and it was consuming too much time getting me nowhere really.
    So i started re-wriing my gui fully in js. This way i can control everything simply with on off states
    1/0. ive made good progress now, and everything is working how i intended it too and the logic is so much simpler to follow. Proberbly faster execution also.