Home > Net >  How to solve the error Operator '*' cannot be applied to operands of type 'float'
How to solve the error Operator '*' cannot be applied to operands of type 'float'

Time:01-15

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SceneFader : MonoBehaviour
{
    #region FIELDS
    public GameObject fadeOutUIGameobjectImage;
    public float fadeSpeed = 3f;
    public SaveLoad saveLoad;

    private Image fadeOutUIImage;
    private DirectoryInfo lastDir;

    private void Start()
    {
        SceneManager.sceneLoaded  = SceneManager_sceneLoaded;
    }

    public enum FadeDirection
    {
        In, //Alpha = 1
        Out // Alpha = 0
    }
    #endregion

    #region FADE

    private float curFadeTime = 0;
    private float alpha = 0;

    public IEnumerator Fade(FadeDirection fadeDirection, float fadeDuration)
    {
        bool fadingIn = fadeDirection == FadeDirection.In;
        alpha  = (Time.deltaTime / fadeDuration) * fadingIn ? 1 : -1;
        alpha = Mathf.Clamp01(alpha); // Make sure alpha is between 0-1!

        // Set image color here.

        curFadeTime  = Time.deltaTime;

        if (curFadeTime < fadeDuration)
        {
            // We are not done fading yet, run the coroutine again!
            yield return null; // Wait until the next frame.
            StartCoroutine(Fade(fadeDirection, 3f));
        }
        else // curFadeTime >= fadeDuration
        {
            curFadeTime = 0; // Reset the fade time for the next fade.
                             // We are done fading. Here you can start loading etc.
                             // The loop ends; we don't run Fade(...) again.
        }
    }

    #endregion

    #region HELPERS

    public IEnumerator FadeAndLoadScene(FadeDirection fadeDirection, string sceneToLoad)
    {
        if (SaveGameManual.manualLoading == false || LevelButtonItem.manualLoading == false)
        {
            yield return Fade(fadeDirection, 3f);
        }
        else
        {
            yield return null;
        }
        SceneManager.LoadSceneAsync(sceneToLoad);
    }
    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        MenuController.newGameClicked = false;

        if (MenuController.LoadSceneForSavedGame == true)
        {
            if (saveLoad == null)
            {
                saveLoad = GameObject.Find("Save Load").GetComponent<SaveLoad>();
            }

            if (SaveGameManual.manualLoading)
            {
                saveLoad.Load(GetLastDir());// <<<<<
                SaveGameManual.manualLoading = false;

            }

            if (LevelButtonItem.manualLoading)
            {
                GetDirsFiles();
            }
        }
    }

    private void SetColorImage(ref float alpha, FadeDirection fadeDirection)
    {
        if (fadeOutUIImage == null)
        {
            fadeOutUIImage = fadeOutUIGameobjectImage.GetComponent<Image>();
        }

        fadeOutUIImage.color = new Color(fadeOutUIImage.color.r, fadeOutUIImage.color.g, fadeOutUIImage.color.b, alpha);
        alpha  = Time.deltaTime * (1.0f / fadeSpeed) * ((fadeDirection == FadeDirection.Out) ? -1 : 1);
    }
    #endregion

    private void GetDirsFiles()
    {
        string savedGamesFolder = string.Format("{0}/Saved Games",
            Application.persistentDataPath);
        string[] dirs = Directory.GetDirectories(savedGamesFolder);
        for (int i = 0; i < dirs.Length; i  ) 
                                              
        {
            if (dirs[i].Contains(LevelButtonItem.saveSlotNumber))
            {
                var files = Directory.GetFiles(dirs[i]);
                saveLoad.Load(files[1]);
            }
        }
    }

    private string GetLastDir()
    {
        string savedGamesFolder = string.Format("{0}/Saved Games",
            Application.persistentDataPath);
        lastDir = new DirectoryInfo(savedGamesFolder).GetDirectories()
                       .OrderByDescending(d => d.LastWriteTimeUtc).First();

        var files = Directory.GetFiles(lastDir.FullName);

        return files[1];
    }
}

Operator '*' cannot be applied to operands of type 'float' and 'bool'

and this error

There is no argument given that corresponds to the required parameter 'fadeDuration' of 'SceneFader.Fade(SceneFader.FadeDirection, float)'

both errors on the line :

alpha  = (Time.deltaTime / fadeDuration) * fadingIn ? 1 : -1;

in the part : (Time.deltaTime / fadeDuration) * fadingIn

what i want to do is to use this script to fade in/out when loading scenes/saved games with time duration and also to use only the Fade method for fade in/out the screen when needed in the game.

the method FadeAndLoadScene was working fine but then i changed the Fade method and also added to the Fade method the time duration variable fadeDuration and since then getting this errors.

CodePudding user response:

In this particular case, it’s a matter of the order of operations. The ? operator isn’t being applied to fadeIn. fadeIn is trying to be applied to the prior float value. To fix the order of operations, try adding some parentheses:

alpha  = (Time.deltaTime / fadeDuration) 
    * ( fadingIn ? 1 : -1);

CodePudding user response:

As far as I know, you can't multiply bool and float variables in C#. What result do you expect to see? If you want cast bool to float, you can use somethings like this:

alpha  = (Time.deltaTime / fadeDuration) * Convert.ToSingle(fadingIn)? 1 : -1;

But I'm not sure it's good practice.

  • Related