0
\$\begingroup\$

I have 4 buttons and they each flash when the button is pressed. However if you were to press a button, then while the button is darker (flashing) click another one, that button then stays dark. I want to reset the color of the sprite before disabling them however it's not working. When the buttons are pressed they no longer flash....

Here is the code:

void Start () {
        easy.GetComponent<SpriteRenderer>().color = color;
    }

    void Update () {
        if(difficultySelected[0] == true){
            enemyBehavior.shotsPerSecond = 0.2f;
            //Enabling the selected button
            easy.enabled = true;

            //Reseting the colors
            normal.GetComponent<SpriteRenderer>().color = color;
            hard.GetComponent<SpriteRenderer>().color = color;
            expert.GetComponent<SpriteRenderer>().color = color;

            //Disabling the 'unselected' buttons
            normal.enabled = false;
            hard.enabled = false;
            expert.enabled = false;

            easy.Play("Difficulty");
        }else if(difficultySelected[1] == true){
            enemyBehavior.shotsPerSecond = 0.5f;
            normal.enabled = true;

            easy.GetComponent<SpriteRenderer>().color = color;
            hard.GetComponent<SpriteRenderer>().color = color;
            expert.GetComponent<SpriteRenderer>().color = color;

            easy.enabled = false;
            hard.enabled = false;
            expert.enabled = false;

            normal.Play("Difficulty");
        }else if(difficultySelected[2] == true){
            enemyBehavior.shotsPerSecond = 1;
            hard.enabled = true;
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can write:

color.a = 1f; // value between 0 and 1, where 1 is opaque

//Resetting the colors
normal.GetComponent<SpriteRenderer>().color = color;
hard.GetComponent<SpriteRenderer>().color = color;
expert.GetComponent<SpriteRenderer>().color = color;
\$\endgroup\$
2
  • \$\begingroup\$ I just tried that, it doesn't make a difference though. They still don't flash when they are in, and when they are removed they work fine, but don't reset color. \$\endgroup\$
    – mr-matt
    Commented Sep 17, 2015 at 6:02
  • \$\begingroup\$ Nevermind, Got it working now, added an answer with my code \$\endgroup\$
    – mr-matt
    Commented Sep 17, 2015 at 6:18
0
\$\begingroup\$

Turns out I the easy, normal etc. variables were just the Animators, not the whole image. I just added another set of variables with the images in and it worked fine. Here is the final code:

    public EnemyBehavior enemyBehavior;
    public bool[] difficultySelected;

    public Animator easy;
    public Animator normal;
    public Animator hard;
    public Animator expert;

    public Image easyButton;
    public Image normalButton;
    public Image hardButton;
    public Image expertButton;

    private Color color;



    public void DifficultySelected(int name){
        difficultySelected[0] = false;
        difficultySelected[1] = false;
        difficultySelected[2] = false;
        difficultySelected[3] = false;
        difficultySelected[name] = true;
    }

    void Update () {
        if(difficultySelected[0] == true){
            enemyBehavior.shotsPerSecond = 0.2f;
            //Enabling the selected button
            easy.enabled = true;

            //Disabling the 'unselected' buttons
            normal.enabled = false;
            hard.enabled = false;
            expert.enabled = false;

            normalButton.color = new Color(1f, 1f, 1f, 1f);
            hardButton.color = new Color(1f, 1f, 1f, 1f);
            expertButton.color = new Color(1f, 1f, 1f, 1f);

            easy.Play("Difficulty");
        }else if(difficultySelected[1] == true){
            enemyBehavior.shotsPerSecond = 0.5f;
            normal.enabled = true;

            easy.enabled = false;
            hard.enabled = false;
            expert.enabled = false;

            easyButton.color = new Color(1f, 1f, 1f, 1f);
            hardButton.color = new Color(1f, 1f, 1f, 1f);
            expertButton.color = new Color(1f, 1f, 1f, 1f);

            normal.Play("Difficulty");
        }else if(difficultySelected[2] == true){
            enemyBehavior.shotsPerSecond = 1;
            hard.enabled = true;

            easy.enabled = false;
            normal.enabled = false;
            expert.enabled = false;

            easyButton.color = new Color(1f, 1f, 1f, 1f);
            normalButton.color = new Color(1f, 1f, 1f, 1f);
            expertButton.color = new Color(1f, 1f, 1f, 1f);

            hard.Play("Difficulty");
        }else if(difficultySelected[3] == true){
            enemyBehavior.shotsPerSecond = 2;
            expert.enabled = true;

            easy.enabled = false;
            normal.enabled = false;
            hard.enabled = false;

            easyButton.color = new Color(1f, 1f, 1f, 1f);
            normalButton.color = new Color(1f, 1f, 1f, 1f);
            hardButton.color = new Color(1f, 1f, 1f, 1f);

            expert.Play("Difficulty");
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .