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

SetBool isn’t working in runtime.

Discussion in 'Animation' started by VirtualCod23, Aug 22, 2021.

  1. VirtualCod23

    VirtualCod23

    Joined:
    Jul 12, 2020
    Posts:
    1
    Hey there!
    I’ve ran into a problem whilst creating a slide in and slide out animation for a panel in the UI. Basically, I’m trying to get the panel to slide in when the “E” key is pressed and to slide out when it is pressed again. I’ve set up the animator with one bool named “isOpen”.
    Code (CSharp):
    1.     bool isOpen = false;
    2.  
    3.     public Animator phoneAnimator;
    4.  
    5.     public void Update()
    6. {
    7. if (Input.GetKeyDown("e") && !isOpen)
    8. {
    9.             phoneAnimator.SetBool("IsOpen", true);
    10.             isOpen = true;
    11. }
    12.        
    13.         if (Input.GetKeyDown("e") && isOpen)
    14. {
    15.             phoneAnimator.SetBool("IsOpen", false);
    16.             isOpen = false;
    17.         }
    18.     }
    I can upload the animators transitions if that would help too!

    Thanks in advance!
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    You actually had me scratching my head for a few minutes haha.

    You need to make the second if statement an "else if" instead. The issue is that you set isOpen to true in the first if statement (making the second if statement true), then go to the second if statement on the same frame and immediately set it back to false.