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

Why does Unity animator makes calling children's SetActive fail?

Discussion in 'Animation' started by Sun-Pengfei, Apr 17, 2018.

  1. Sun-Pengfei

    Sun-Pengfei

    Joined:
    Nov 12, 2015
    Posts:
    35
    I'm working on a gameObject prefab to show animations of getting a yellow star(change from grey to yellow) or losing a yellow star(change from yellow to grey), and it should also support showing only 2 status according to data(show yellow or grey star at the beginning with no animation).

    My gameObject structure:
    ObjectA
    yellowStarObj(image)
    greyStarObj(image)
    testImageObj(image)

    OjbectA has a script ClassA, and an animator, with 5 animations called: DoNothing, Hide, Show, Hidden, Shown.
    "DoNothing" does nothing, and it is the default animation.
    "Show" set the yellowStarObject to active, grey to inactive, and then change to Shown animation.
    "Shown" set the yellowStarObject to active, grey to inactive and loops.
    "Hide" and "Hidden" do the opposite of "Show" and "Shown".

    In the script, I have these following code to manually call them and change the object to a start status according to data(shown or hidden in the beginning).

    Code (CSharp):
    1.     public void Show()
    2.     {
    3.         animator.enabled = false; // if comment this line out, the two lines of "SetActive" won't work
    4.         print("before setActive, yellowStarObj.activeInHierarchy = " + yellowStarObj.activeInHierarchy);
    5.         yellowStarObj.SetActive(true);
    6.         print("after setActive, yellowStarObj.activeInHierarchy = " + yellowStarObj.activeInHierarchy);
    7.         greyStarObj.SetActive(false);
    8.         testImageObj.SetActive(true); // this line always works, I think it's because it has nothing to do with the animator
    9.     }
    10.  
    11.     public void Hide()
    12.     {
    13.         animator.enabled = false; // same
    14.         print("before setActive, yellowStarObj.activeInHierarchy = " + yellowStarObj.activeInHierarchy);
    15.         yellowStarObj.SetActive(false);
    16.         print("after setActive, yellowStarObj.activeInHierarchy = " + yellowStarObj.activeInHierarchy);
    17.         greyStarObj.SetActive(true);
    18.         testImageObj.SetActive(false); // same
    19.     }
    Please see the comment in the code. Notice the outcome of the printing lines are always show the correct line(with animator enabled). I think it means the two gameObject setActive are working fine, but somehow the animator just changed them back to prefab status. It's very similar to my other designing approaches describe in the following paragraph.

    Reason why I don't use a parameter for the animator and use the two animations(hidden/shown) to show the status, or why I don't just call Play("hidden", 0, 0) is it doesn't work. Right after I set the parameter in Start() it got changed by the animator to default. It only works if I set it after at least two frames after the animator completed initialization. The same when I use calling `Play`; When called play, the animation got triggered however it then switched to the default animation at the same time(by the animator I guess).

    So what should I do to let the animator show the right animation in the beginning? I'm willing to change my design pattern, Thanks!
     
  2. artaka

    artaka

    Joined:
    Feb 19, 2013
    Posts:
    128
    First thing I would check is whether your animation clips contains keys for activating/deactivate the stars. If they do, then most likely that's what's causing your issue.
     
  3. Sun-Pengfei

    Sun-Pengfei

    Joined:
    Nov 12, 2015
    Posts:
    35
    Hi artaka, thanks for your advice.
    Yes four clips(except for the clip "DoNothing") contains keys that activates/deactivates stars, that's exactly what they do. My problem is that those four clips should not be triggered(and they are not, proven by adding call back keys). The problem exists under the situation that only the default clip "DoNothing" is triggered.