Cannot implicitly convert type 'bool' to 'UnityEngine.Rendering.PostProcessing.Vignette'

Just after some help. I’ve setup a UI with a toggle and slider to control Vignette on/off and intensity. I’m trying to use PlayerPrefs to set and get toggle on/off. but it is throwing the below error.

The code I’m using is mean’t to convert bool to int, then int back to bool…

error CS0029: Cannot implicitly convert type ‘bool’ to ‘UnityEngine.Rendering.PostProcessing.Vignette’

the code is below and the error line is the one in bold…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;

public class VignetteTutorial: MonoBehaviour
{
private PostProcessVolume _postProcessVolume;
private Vignette _vignette;

//[Header(“UI Values”)]
//[SerializeField] private Text VignetteIntensityVvalueText;

// Start is called before the first frame update
void Start()
{
_vignette = PlayerPrefs.GetInt(“boolDataName”)==1?true:false;
//_vignette = PlayerPrefs.GetInt(“name”) == 1 ? true : false;
//_vignette = (PlayerPrefs.GetInt(“name”) ! = 0);
_postProcessVolume = GetComponent();
_postProcessVolume.profile.TryGetSettings(out _vignette);
}

public void VignetteOnOff(bool on)
{
if (on)
{
_vignette.active = true;
}
else
{
_vignette.active = false;
}
}

public void VignetteIntensity(float sliderValue)
{
_vignette.intensity.value = sliderValue;
//VignetteIntensityVvalueText.text = sliderValue.ToString(“0”);
}

void Update()
{
PlayerPrefs.SetInt(“boolDataName”, _vignette ? 1 : 0);
//PlayerPrefs.SetInt(“name”, (_vignette ? 1 : 0));
PlayerPrefs.Save();
}
}

You cannot just assign to a field that is defined as one type with a value of a completely different type. You’ve got a field of type “Vignette” (postprocessing class) and you want to assign a bool (yes/no, true/false). That makes no sense at all. The error clearly tells you this so I’m not sure what your post is asking.

Doing “_vignette ? 1 : 0” will just indicate if that field has a reference to a class or not.

btw, here’s how to post code on the forums: Using code tags properly

Sorry I’m c# noob.

The code works without the playerprefs lines. It enables me to toggle vignette on/off with a ui toggle box. What I was trying to do is save the toggle option, either on or off. So on game restart it reloads the either on or off depending on what’s selected…

BTW playerprefs doesn’t accept bool, so I was attempting to convert bool to int… from code found on net

Don’t you want to first get the Vignette reference then set it active/inactive from preferences?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;

public class VignetteTutorial: MonoBehaviour
{
    private PostProcessVolume _postProcessVolume;
    private Vignette _vignette;

    // Start is called before the first frame update
    void Start()
    {
        _postProcessVolume = GetComponent<PostProcessVolume>();
        _postProcessVolume.profile.TryGetSettings(out _vignette);
        _vignette.active = PlayerPrefs.GetInt("boolDataName") == 1;
    }

    public void VignetteOnOff(bool flag)
    {
        _vignette.active = flag;

        PlayerPrefs.SetInt("boolDataName", flag ? 1 : 0);
        PlayerPrefs.Save();
    }
}

NOTE: I have never used the post-processing stuff so I have no idea if there’s even an “active” property but the above corrects what you were trying to do by the looks of it.

One thing at a time. Figure out how to toggle the Vignette first, then worry about using PlayerPrefs to do it.

As Melv said, you are trying to store the wrong kind of thing into a variable. None of these lines make sense:

int foo = true;
string bar = false;
GameObject baz = true;

All of those lines will give you an error, because the types are not compatible. The same thing happens when you try to do _vignette = true;_vignette is a variable that holds a Vignette!

And as Melv also said, we don’t really know exactly what a Vignette is here – but if it has a field called “doTheThing”, then this would be valid:

_vignette.doTheThing = true;

Here’s the original code, before I tried to use PlayerPrefs to store and reload the toggle…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;

public class VignetteTutorial: MonoBehaviour
{
    private PostProcessVolume _postProcessVolume;
    private Vignette _vignette;

 

    // Start is called before the first frame update
    void Start()
    {
        _postProcessVolume = GetComponent<PostProcessVolume>();
        _postProcessVolume.profile.TryGetSettings(out _vignette);
    }

 
    public void VignetteOnOff(bool on)
    {
        if (on)
        {
            _vignette.active = true;
        }
        else
        {
            _vignette.active = false;
        }
    }

    public void VignetteIntensity(float sliderValue)
    {
        _vignette.intensity.value = sliderValue;
     
    }

    void Update()
    {
     
    }
}

The Script is on an empty game object called PostProcessingFolder this also has the Post Process Volume on it which includes Ambient Occlusion and Vignette.

The Toggle is on UI, it has a On Value Changed (Boolean) pictures below to explain it…

Below is a YouTube showing it working with the original code above. What I want to do is store any changes like Toggle clicked, then when the game restarts it automatically sets the last used options and values.

1 Like

Thanks MelvMay.

What you posted does correct the toggle issue, the only other issue is updating the UI on play enter to show the updated PlayerPrefs which are loaded…

Hopefully my previous post will show better what I’m trying to do…