1
votes

I'm doing an android 2d platformer. In the Animator everything is set up. On applicaton start does everything right but when I click on the button the bool parameter does not change so my animation won't play backwards. But when I tick the bool parameter in the animator the animation plays right as it should. No console errors. Here is my script.

I was using this tutorial and from it I just needed the first part (the "Start game" animation part). I followed every step. Probably I'm missing something.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    public Animator optionsButton;

    public void OpenSettings()
    {
        optionsButton.SetBool("isHidden", false);
    }
}

My goal is to play the animation backward when the option button is pressed and I want to change the bool parameter through a script.

1
Is the OpenSettins() method being called? add a debug message to it to make sure it is being called when you click the button. I say this as you might not have asigned the buttons onClick method in the editor. - akaBase
I used Debug.Log(Button Clicked); but same happend; when I clicked the button the parameter wont change but the debug message is written in the console. I expected at least an error message but there is none. - Jozef Benko
check that the animator isn't null and that the spelling of the bool parameter is correct because there is nothing wrong with the code. - akaBase
Can you advice me how can I check the animator isn't null? Well we can agree on that because I looked for a sollution and cant find anything. It is so weird. As I looked on the internet and can be the problem that one of a states in the animator is a copy of the default state? Found some kind of legacy issue and root issues but non of them worked. I have a feeling that it will be a bug or something pretty simple. I vote for the second :D - Jozef Benko
You can use a normal animator == null to check if it is null or not. I am starting to think that you have the single clip in you Animator and it is running the animation when you start the game/scene and then finishes. If so you need to restart it using options.Play("nameofnanimation"), also look into CrossFade - akaBase

1 Answers

1
votes

The problem was that the script was trying to change the bool on a canvas child element object. And there wasn't a bool. So I altered the existing script to get it. Worked perfectly.

public class UIManager : MonoBehaviour
{

    public Animator optionsButton;
    public bool hide;

  private void Start()
   {
       //to find the parent element - canvas - which has a bool
       optionsButton = GameObject.Find("MainMenu").GetComponent<Animator>();
   }

    public void OpenSettings()
    {
        hide = true;
        optionsButton.SetBool("AnimOut", hide);

        Debug.Log("I'm finaly working!");
    }

}