Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[SOLVED] UI CANVAS : GameObject.GetComponentsInChildren

Discussion in 'UGUI & TextMesh Pro' started by Deleted User, Dec 30, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    The code I use allows to enabled/disable a canvas when the camera see a QRCode, until now I used the code below:

    Code (CSharp):
    1.         //enable canvas
    2.         Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas));
    3.         {
    4.             canvasObject.enabled = true;
    5.         }
    Code (CSharp):
    1.         //disable canvas
    2.         Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas));
    3.         {
    4.             canvasObject.enabled = false;
    5.         }
    The problem is with several different canvas, Unity enabled/disable just the first canvas found...

    I would like to use the function GameObject.GetComponentsInChildren with the canvas instead of the function FindObjectOfType ?

    I tried with the help offered by Unity3D website, but without success. Thank you in advance for your help !
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    if you put all your canvases under a single game object, and your script knows about that parent gameobject then the GetComponentInChildren will work for you.

    The other option is to specify a list of Canvases to run through. if you know them all at edit time you can do it via the inspector. If you need it at run time you will need to come up with a way to gather all the canvases
     
  3. Deleted User

    Deleted User

    Guest

    Thanks @phil-Unity for you answer, but i don't understand your second sentence, i started in Unity3D ;) I would like use the function GetComponentsInChildren with canvasses, but i don't know write the code...

    If i put my canvas in a empty "GameObject", this code is correct ?

    Code (CSharp):
    1.     private void OnTrackingFound()
    2.     {
    3.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    4.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    5.  
    6.         //enable canvas
    7.                public GameObject[] GameObject;
    8.                void Example() {
    9.                         GameObject = gameObject.GetComponentsInChildren<GameObject>();
    10.         {
    11.             gameObject.enabled = true;
    12.         }
    13.         }
    14.     }
     
    Last edited by a moderator: Dec 30, 2014
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    I usually dont do this as it tends to come back as i dont test the example code i write but something like the following should do what you need.

    Code (CSharp):
    1. public class MyClass
    2. {
    3.     public GameObject parentObject;
    4.  
    5.     public void MyFunction()
    6.     {
    7.         Canvas[] canvasObjects = parentObject.GetComponentInChildren<Canvas>();
    8.        
    9.         foreach(Canvas canvas in canvasObjects)
    10.         {
    11.             canvas.gameobject.enabled = true;
    12.         }
    13.     }
    14. }
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    Thanks @phil-Unity but it doesn't work :confused: There are 2 error messages for each "Canvas function" :

    Assets/Qualcomm Augmented Reality/Scripts/DefaultTrackableEventHandler.cs(72,26): error CS0029: Cannot implicitly convert type `UnityEngine.Canvas' to `UnityEngine.Canvas[]'

    Assets/Qualcomm Augmented Reality/Scripts/DefaultTrackableEventHandler.cs(77,32): error CS1061: Type `UnityEngine.Canvas' does not contain a definition for `gameobject' and no extension method `gameobject' of type `UnityEngine.Canvas' could be found (are you missing a using directive or an assembly reference?)

    COMPLETE CODE

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. /// <summary>
    4. /// A custom handler that implements the ITrackableEventHandler interface.
    5. /// </summary>
    6. public class DefaultTrackableEventHandler : MonoBehaviour,
    7.                                             ITrackableEventHandler
    8. {
    9.     #region PRIVATE_MEMBER_VARIABLES
    10.     private TrackableBehaviour mTrackableBehaviour;
    11.  
    12.     #endregion // PRIVATE_MEMBER_VARIABLES
    13.  
    14.  
    15.  
    16.     #region UNTIY_MONOBEHAVIOUR_METHODS
    17.  
    18.     void Start()
    19.     {
    20.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    21.         if (mTrackableBehaviour)
    22.         {
    23.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    24.         }
    25.     }
    26.  
    27.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    28.  
    29.  
    30.  
    31.     #region PUBLIC_METHODS
    32.  
    33.     /// <summary>
    34.     /// Implementation of the ITrackableEventHandler function called when the
    35.     /// tracking state changes.
    36.     /// </summary>
    37.     public GameObject parentObject;
    38.     public void OnTrackableStateChanged(
    39.                                     TrackableBehaviour.Status previousStatus,
    40.                                     TrackableBehaviour.Status newStatus)
    41.     {
    42.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    43.             newStatus == TrackableBehaviour.Status.TRACKED ||
    44.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    45.         {
    46.             OnTrackingFound();
    47.         }
    48.         else
    49.         {
    50.             OnTrackingLost();
    51.         }
    52.     }
    53.  
    54.     #endregion // PUBLIC_METHODS
    55.  
    56.  
    57.  
    58.     #region PRIVATE_METHODS
    59.  
    60.  
    61.     private void OnTrackingFound()
    62.     {
    63.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    64.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    65.         Canvas[] canvasObjects = parentObject.GetComponentInChildren<Canvas>();
    66.  
    67.  
    68.  
    69.         //enable canvas
    70.  
    71.         foreach(Canvas canvas in canvasObjects)
    72.  
    73.         {
    74.  
    75.             canvas.gameobject.enabled = true;
    76.  
    77.         }
    78.  
    79.         // Enable rendering:
    80.         foreach (Renderer component in rendererComponents)
    81.         {
    82.             component.enabled = true;
    83.         }
    84.  
    85.         // Enable colliders:
    86.         foreach (Collider component in colliderComponents)
    87.         {
    88.             component.enabled = true;
    89.         }
    90.  
    91.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    92.     }
    93.  
    94.  
    95.     private void OnTrackingLost()
    96.     {
    97.         Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    98.         Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    99.         Canvas[] canvasObjects = parentObject.GetComponentInChildren<Canvas>();
    100.  
    101.  
    102.  
    103.         //disable canvas
    104.  
    105.         foreach(Canvas canvas in canvasObjects)
    106.  
    107.         {
    108.  
    109.             canvas.gameobject.enabled = false;
    110.  
    111.         }
    112.  
    113.         // Disable rendering:
    114.         foreach (Renderer component in rendererComponents)
    115.         {
    116.             component.enabled = false;
    117.         }
    118.  
    119.         // Disable colliders:
    120.         foreach (Collider component in colliderComponents)
    121.         {
    122.             component.enabled = false;
    123.         }
    124.  
    125.         Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    126.     }
    127.  
    128.     #endregion // PRIVATE_METHODS
    129. }
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Canvas[] canvasObjects = parentObject.GetComponentInChildren<Canvas>(); should be Canvas[] canvasObjects = parentObject.GetComponentsInChildren<Canvas>(); (notice the s on components)


    canvas.gameobject.enabled=true; needs to be canvas.gameObject.enabled=true; (notice the capital O in gameObject).

    This is the exact reason i dont try to write code examples as if i miss one letter or capitalization it always comes back :(.
     
    Deleted User likes this.
  7. Deleted User

    Deleted User

    Guest

    Thanks a lot @phil-Unity, it works !

    I custom a little bit your code :)

    Code (CSharp):
    1.         Canvas[] canvasObjects = GetComponentsInChildren<Canvas>();
    2.  
    3.         //enable canvas
    4.         foreach(Canvas canvas in canvasObjects)
    5.         {
    6.             canvas.enabled = true;
    7.         }
    Code (CSharp):
    1.         Canvas[] canvasObjects = GetComponentsInChildren<Canvas>();
    2.  
    3.         //disable canvas
    4.         foreach(Canvas canvas in canvasObjects)
    5.         {
    6.             canvas.enabled = false;
    7.         }