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
  3. Dismiss Notice

Question Using SetActive on ARFaceManager prefab child objects

Discussion in 'Getting Started' started by CaptainQuality, Mar 18, 2024.

  1. CaptainQuality

    CaptainQuality

    Joined:
    Mar 10, 2015
    Posts:
    2
    I am working on the ocean-themed interactive face filter lesson (https://learn.unity.com/tutorial/submission-ocean-themed-face-filter) and I want to implement the UI functionality using C# scripts. My goal is to be able to show and hide child objects of the ARFace's prefab object using a script.

    I created the following script that I have assigned to the first button in my UI. I am able to get the reference to myCube, and calling SetActive(false) in the Start() function seems to work, hiding the object from view on startup. However, when I tap the button, buttonTapped() does get called but nothing happens. The activeSelf property of myCube is indeed true, but I cannot see anything.

    What am I doing wrong? I also tried getting the MeshRenderer component and toggling the enabled property but that does nothing. Is it because the object is a child of the ARFace's prefab object? Am I not retaining the reference properly?

    Code (CSharp):
    1.  
    2. GameObject myCube;
    3. public ARFaceManager faceManager;  // assigned from hierarchy
    4.  
    5. void Start()
    6. {                
    7.      Transform parentTransform = faceManager.facePrefab.transform;
    8.      myCube = parentTransform.Find("MyCube").gameObject;
    9.      myCube..SetActive(false);
    10. }
    11.  
    12. public void buttonTapped()
    13. {
    14.      myCube.SetActive(true);
    15. }
    16.  
    17.  
     
  2. CaptainQuality

    CaptainQuality

    Joined:
    Mar 10, 2015
    Posts:
    2
    Answered my own question. This works:
    Code (CSharp):
    1.     public ARFaceManager faceManager;
    2.     GameObject myCube;
    3.  
    4.     void Start()
    5.     {      
    6.         // Subscribe to face detection event
    7.         faceManager.facesChanged += OnFacesChanged;
    8.     }
    9.  
    10.     void OnFacesChanged(ARFacesChangedEventArgs eventArgs)
    11.     {
    12.         if (eventArgs.added.Count > 0)
    13.         {
    14.             myCube = GameObject.Find("TestCube");
    15.         }
    16.     }
    17.