GUI Hide/unhide with animation

Hi guys,

first time poster here and as usual I am very new at all this…

I have set up some UI that I want to hide and then show on key press (space), I have set up the animation and script (not sure if I should be using a controller for this) but I am getting “Possible mistaken empty statement (CS0642)” errors when I debug the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HideGUI : MonoBehaviour
    {

    private Animation aniComponent;
    private bool StatsUp = false;

        void Start ()
        {
            aniComponent = gameObject.GetComponent<Animation>();
            Debug.Log("Start void triggered");
        }

    void Update ()
        {
            if(Input.GetKeyDown (KeyCode.Space));
                    StatsUp = !StatsUp;
                    Debug.Log ("Space input detected, setting StatsUp to opposite value");

        if (StatsUp == true);
            aniComponent.Play ("CharSheet1");
            Debug.Log ("Play animation forward");

        if (StatsUp == false);
            aniComponent.Rewind ("CharSheet1");
            Debug.Log ("Play animation backward");

        }
}

I should also probably mention that this code doesn’t work :frowning:

Hope you can help!

It looks like you’re putting a semi colon at the end of your ‘if’ statement - that means that the if statement will end at that point and all the code after it will run anyway.

Instead of doing this:

if (StatsUp == false);
            aniComponent.Rewind ("CharSheet1");
            Debug.Log ("Play animation backward");

you should be doing this for all your ‘if’ statements:

if (StatsUp == false)
{
    aniComponent.Rewind ("CharSheet1");
    Debug.Log ("Play animation backward");
}
1 Like

That fixed the initial issue, so thanks very much… Now onto the next part that is doing my head in… playing the actual animations when I press space…

My current code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HideGUI : MonoBehaviour
    {

    private Animation aniComponent;
    private bool StatsUp = false;
    public AnimationClip Animation1;
    public AnimationClip Animation2;

        void Start ()
        {
            aniComponent = gameObject.GetComponent<Animation>();
            Debug.Log("Start void triggered");
        }

    void Update ()
        {
            if(Input.GetKeyDown (KeyCode.Space))
                    StatsUp = !StatsUp;
                    Debug.Log ("Space input detected, setting StatsUp to opposite value");

        if (StatsUp == true)
            aniComponent.Play("Animation1");
            Debug.Log ("Play animation forward");

        if (StatsUp == false)
            aniComponent.Rewind("Animation2");
            Debug.Log ("Play animation backward");

        }
}

I have added both the disappear and reappear animation as ‘Animation1’ & ‘Animation2’, these animations clips are set in the inspector but when I press play, then hit the space key I get an error stating ‘The animation state Animation1 could not be played because it couldn’t be found!’

Thanks again!

Linking the animation clips, as you’ve done so doesn’t matter (at least, not as your code is right now). Since the animation.Play() takes a string argument, your variables aren’t even being used. Let’s say you just want to use the names - is Animation1 the actual name of the clip you linked in the inspector?

Also, I wanted to point out that you are still missing your parentheses around your ‘if’ statements. As you have it written currently, the Debug.Logs will always print because the ‘if’ statements execute 1 line afterwards, with the brackets.

if(a == 1) 
 c = 5;
 Debug.Log ("a was 1");  // this will always print, even if a is 15, or 75, or anything.
// however..
if(a == 1) {
  c = 5;
  Debug.Log("a was 1"); // will only print if a was 1.
 }

One other comment is that for your: if(StatsUp == true) then later if(StatesUp == false) could just be changed to “else”
(and if it were a longer chain, ‘else if’ instead).

Hi, I think I have fixed the parentheses now…

if (Input.GetKeyDown (KeyCode.Space)) {
                StatsUp = !StatsUp;
                Debug.Log ("Space input detected, setting StatsUp to opposite value");
            }

As an example… Now, with the animation, I did originally have the actual animation names, but again I get this error:
“The animation state CharSheet1 could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘CharSheet1’ or call this function only for existing animations”

Now, I have actually created an Animator with 3 layers showing what needs to happen, but again, I just can’t figure out how to call the specific layers from within the script…

Good for fixing the brackets, but now you changed to animator from animation?
I have never really used layers. Only transitions, myself (limited experience).
If you have only 3 actions, you could probably setup very simple transitions for them, which you could setup with triggers/bools.

I mean within the Unity UI, the built in Animator (not within the code)

To elaborate, Charsheet0 is the character sheet showing, charsheet1 is the animation to closing and charsheet01 is the closed character sheet… I also have a seperate animation (charsheet2) which is the reverse of charsheet1 (from closed to open). Ideally I would like for this effect within the script, but happy to get any open/close script to work right now… :S

Okay, but you’re still using the Animation component on the game object?
I ask this because there’s a component called “Animator”, as well.

Again, I don’t know layers, but you said you used to use names and that didn’t work… that part I don’t get. If the names are right, and you have clips with those names on the game object, as far as I know, that’s all there is to it.
What’s special about these animations, by the way? Just hide/show, is that it?
That could be done in code very easily, also… I mean, if it means solving this otherwise confusing situation for you… :slight_smile:

As I put in my initial post, I am very new to unity (and programming altogether…) so I don’t really the best way to do things…

I have a UI set up, its positioned on the screen like so…

all the animation does is move the Pos X from -170 to -370 and rotate the Y axis 90 degrees to effectively hide it… then reverse to bring it back into view and re-position back to original Pos X.

Ah okay, so it’s a bit more than just disabling it (it moves).
Not sure what else to tell you or what I may have missed… you should be able to play the animation by name.
I hope you find a solution :slight_smile:

yeah, it moves, otherwise I wouldn’t have bothered with an animation, but I thought it would look cooler… I know I can do a rect transform within the script though, so maybe I need to give that a go… something for tomorrow.

Thanks for your help Methos!

Okay, so I tried to create an animation + 1 clip in a project to see how it works.
Did you add these animation clips to the animation component itself in the inspector?
When I added one, and called it by name it worked. I thought of this after we chatted yesterday/earlier, and I wondered if maybe that’s why it hadn’t worked for you. There’s a slot for just 1, which you can play without any name, but if you use the ‘Animations’ part (where you can put more than 1), then you call them by name, I think. :slight_smile:

Unfortunately, I have been doing that… ugh.

Still can’t call them by name though

Okay, now truly out of options :slight_smile: lol
If you’d like you could try to attach the project here and I’d look at it. :slight_smile:
If for any reason you can’t upload it here (sometimes i see people can, other times not), you could post it in a dropbox link. Can’t say I can fix it, but I could try. Either way, good luck :slight_smile:

OMG, I GOT IT TO WORK!!! YAY

Oh , hey, nice :slight_smile: What’d ya do? lol

those damn parenthesis… let me update my final code…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HideGUI : MonoBehaviour
    {

    private Animation aniComponent;
    private bool StatsUp = false;

        void Start ()
        {
            aniComponent = gameObject.GetComponent<Animation>();
            Debug.Log("Start void triggered");
        }

    void Update ()
        {
        if (Input.GetKeyDown (KeyCode.Space)) {
                StatsUp = !StatsUp;
                Debug.Log ("Space input detected, setting StatsUp to opposite value");

                if (StatsUp == true) {
                    aniComponent.Play ("CShide");
                    aniComponent.PlayQueued ("CShidden");
                    Debug.Log ("Play animation forward");
                }

                if (StatsUp == false) {
                    aniComponent.Play ("CSunhide");
                    aniComponent.PlayQueued ("CSvisible");
                    Debug.Log ("Play animation backward");
                }
            }
        }
}

basically included all 3 if statements within the one { } and voila… (yes, I also renamed the animations and made them legacy but not sure if that was part of it…)

also just tested and I don’t actually need the CSvisible and CShidden animations (commented out and the animation plays fine without them, so even better)