Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enable GUI Buttons through Script

Discussion in 'Scripting' started by PatrickAllen, Jul 22, 2014.

  1. PatrickAllen

    PatrickAllen

    Joined:
    May 6, 2014
    Posts:
    16
    Hello,

    I'm working on an Augmented Reality application using Vuforia AR and Unity. I've got GUI "Next" and "Reset" buttons that I only want visible under certain conditions.

    My Button Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NextButton2 : MonoBehaviour
    5. {
    6. public int nextcounter=0; //Init counter at zero
    7. public GUIStyle customButton;
    8.  
    9. public bool ShowButton = true;
    10.  
    11.     void OnGUI ()
    12.     {
    13.  
    14.         if (ShowButton)
    15.         {
    16.                 // Make a background box
    17.             //GUI.Box(new Rect(0,0,0.1f*Screen.width,0.16f*Screen.height), "Press Next");
    18.            
    19.             // Make the first button. If it is pressed, the variable will be increased
    20.             if(GUI.Button(new Rect(0.01f*Screen.width,0.01f*Screen.height,0.1f*Screen.width,0.1f*Screen.height), "Next", customButton)) {
    21.                 // This code executed when Button is clicked
    22.                 nextcounter ++;
    23.                 Debug.Log ("counter = " + nextcounter);
    24.             }
    25.             // Make the second button. If it is pressed, the variable will be reset
    26.             if(GUI.Button(new Rect(0.01f*Screen.width,0.15f*Screen.height,0.1f*Screen.width,0.1f*Screen.height), "Reset", customButton)) {
    27.                 // This code executed when Button is clicked
    28.                 nextcounter=0;
    29.                 Debug.Log ("counter = " + nextcounter);
    30.             }
    31.         }
    32. }
    33. }
    In my testing, toggling the ShowButton Boolean hides my GUI Buttons the way I want.

    Now I need to set the ShowButton Boolean from the DefaultTrackableEventHandler Script.

    Code (CSharp):
    1. /*==============================================================================
    2. Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    3. All Rights Reserved.
    4. Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
    5. ==============================================================================*/
    6.  
    7. using UnityEngine;
    8.  
    9. /// <summary>
    10. /// A custom handler that implements the ITrackableEventHandler interface.
    11. /// </summary>
    12. public class DefaultTrackableEventHandler : MonoBehaviour,
    13.                                             ITrackableEventHandler
    14. {
    15.  
    16.     // public var
    17.     public string itstatus;
    18. [U][B]    public GameObject player;[/B][/U]
    19.  
    20.     #region PRIVATE_MEMBER_VARIABLES
    21.     private TrackableBehaviour mTrackableBehaviour;
    22. [U][B]
    23.     private NextButton2 playerScript;[/B][/U]
    24.    
    25.     #endregion // PRIVATE_MEMBER_VARIABLES
    26.  
    27.  
    28.  
    29.     #region UNTIY_MONOBEHAVIOUR_METHODS
    30.    
    31.     void Start()
    32.     {
    33.         itstatus = "lost";
    34.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    35.         if (mTrackableBehaviour)
    36.         {
    37.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    38.         }
    39.     }
    40.  
    41.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    42.  
    43.  
    44.  
    45.     #region PUBLIC_METHODS
    46.  
    47.     /// <summary>
    48.     /// Implementation of the ITrackableEventHandler function called when the
    49.     /// tracking state changes.
    50.     /// </summary>
    51.     public void OnTrackableStateChanged(
    52.                                     TrackableBehaviour.Status previousStatus,
    53.                                     TrackableBehaviour.Status newStatus)
    54.     {
    55.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    56.             newStatus == TrackableBehaviour.Status.TRACKED ||
    57.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    58.         {
    59.             OnTrackingFound();
    60.         }
    61.         else
    62.         {
    63.             OnTrackingLost();
    64.         }
    65.     }
    66.  
    67.     #endregion // PUBLIC_METHODS
    68.  
    69.  
    70.  
    71.     #region PRIVATE_METHODS
    72.  
    73.  
    74.     private void OnTrackingFound()
    75.     {
    76.        [U][B] playerScript = player.GetComponent<NextButton2> ().ShowButton = true;[/B][/U]
    77.         itstatus = "found";
    78.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    79.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    80.  
    81.         // Enable rendering:
    82.         foreach (Renderer component in rendererComponents)
    83.         {
    84.             component.enabled = true;
    85.         }
    86.  
    87.         // Enable colliders:
    88.         foreach (Collider component in colliderComponents)
    89.         {
    90.             component.enabled = true;
    91.         }
    92.  
    93.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    94.     }
    95.  
    96.  
    97.     private void OnTrackingLost()
    98.     {
    99.         itstatus = "lost";
    100.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    101.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    102.  
    103.         // Disable rendering:
    104.         foreach (Renderer component in rendererComponents)
    105.         {
    106.             component.enabled = false;
    107.         }
    108.  
    109.         // Disable colliders:
    110.         foreach (Collider component in colliderComponents)
    111.         {
    112.             component.enabled = false;
    113.         }
    114.  
    115.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    116.     }
    117.  
    118.     #endregion // PRIVATE_METHODS
    119. }
    120.  
    I've bolded and underlined the code I'm having trouble with. Basically all I want to do is set that Boolean true OnTrackingFound and set it to false OnTrackingLost. I'm new to Unity and C# and searching high and low, and so far I can't figure out what I need to do in my second script to set the public Boolean of the first.

    Can anyone help?

    Thanks.
     
  2. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    basically you want to change a boolean of a different script, then you can do something like this:

    Code (CSharp):
    1.  
    2. //create an empty NextButton2 variable
    3. public GameObject player;
    4. private NextButton2 playerScript;
    5.  
    6. void Start(){
    7. //Assign your script to that variable
    8. playerScript = player.GetComponent<NextButton2>();
    9.  
    10. }
    11. //...............................
    12. void OnTrackingFound(){
    13. playerScript.ShowButton = true;
    14. }
    15.  
    16. void OnTrackingLost(){
    17. playerScript.ShowButton = false;
    18. }
    Hope it helps
     
    PatrickAllen likes this.
  3. PatrickAllen

    PatrickAllen

    Joined:
    May 6, 2014
    Posts:
    16
    Thanks so much!

    At first it seemed like this wasn't working. But I hadn't assigned the Player to my ImageTarget in the Inspector. As soon as I did that, it just worked.

    Hopefully I'll be able to adapt this to some other objects and turn their renderers off when ImageTracking is lost. This works by default, but I'm using a counter to enable my AR objects in a particular order. When I do that, it messes with the default handling that Vuforia does. I think I can see how to make this work now though.

    Thanks again. :)
     
    raycosantana likes this.