Using if functions on UI

Hello,

I am hoping someone might be able to give me some insight into where I might be going wrong here.

The basic premise is that in a mobile VR experience I am trying to use gaze input to trigger actions, very simple actions that fade-in or fade-out panels etc.

This is a simple process with 2 separate buttons, but I am trying to experiment with using a single button to change the state of the panel, using if statements to determine how it should react dependant upon the current state.

I am however missing something, and as I am reasonably new to programming I would be very appreciative if anyone can help.

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

public class GroupFaderState : MonoBehaviour {

    public CanvasGroup uiElement;

    bool _panelActive;

    private void Start()
    {
        uiElement.alpha = 0;
        _panelActive = false;
    }


    public void ChangeState()
    {
        if (_panelActive = false)
        {
            StartCoroutine(FadeCanvasGroup(uiElement, uiElement.alpha, 1));
        }
        else if (_panelActive = true)
        {
            StartCoroutine(FadeCanvasGroup(uiElement, uiElement.alpha, 0));
        }
        else return;
       
    }

    public IEnumerator FadeCanvasGroup(CanvasGroup canvasGroup, float start, float end, float lerpTime = 0.5f)
    {
        float _timeStartedLerp = Time.time;
        float timeSinceStarted = Time.time - _timeStartedLerp;
        float percentageComplete = timeSinceStarted / lerpTime;

        while(true)
        {
            timeSinceStarted = Time.time - _timeStartedLerp;
            percentageComplete = timeSinceStarted / lerpTime;

            float currentValue = Mathf.Lerp(start, end, percentageComplete);

            canvasGroup.alpha = currentValue;

            if (percentageComplete >= 1) break;

            yield return new WaitForEndOfFrame();
        }

        print("done");
    }
}

Hi, you havent really described what your problem is. You are trying something, where does that fail? What happens, what do you expect to happen? What did you try, what did you search for?

I don’t see you changing _panelActive to true anywhere. Also, be careful you don’t trigger your coroutine multiple times as that can create issues as well.

And as @TJHeuvel-net mentions, more info about what is failing is always helpful.

Your are never setting _panelActive to true or false at the end of your fade.

Thank you all for your help. I almost leaped out of my seat once I realised what I had missed. Not only had I failed to declare the state to true or false, I also forgot to put == on the conditional statement. All working now, thank you.