Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by mickstv1972, Jan 7, 2023.

  1. mickstv1972

    mickstv1972

    Joined:
    Apr 14, 2022
    Posts:
    15
    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>();
    _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();
    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    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: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. mickstv1972

    mickstv1972

    Joined:
    Apr 14, 2022
    Posts:
    15
    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
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Don't you want to first get the Vignette reference then set it active/inactive from preferences?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.PostProcessing;
    5. using UnityEngine.UI;
    6.  
    7. public class VignetteTutorial: MonoBehaviour
    8. {
    9.     private PostProcessVolume _postProcessVolume;
    10.     private Vignette _vignette;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         _postProcessVolume = GetComponent<PostProcessVolume>();
    16.         _postProcessVolume.profile.TryGetSettings(out _vignette);
    17.         _vignette.active = PlayerPrefs.GetInt("boolDataName") == 1;
    18.     }
    19.  
    20.     public void VignetteOnOff(bool flag)
    21.     {
    22.         _vignette.active = flag;
    23.  
    24.         PlayerPrefs.SetInt("boolDataName", flag ? 1 : 0);
    25.         PlayerPrefs.Save();
    26.     }
    27. }
    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.
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    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:

    Code (CSharp):
    1. int foo = true;
    2. string bar = false;
    3. 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:

    Code (CSharp):
    1. _vignette.doTheThing = true;
     
  6. mickstv1972

    mickstv1972

    Joined:
    Apr 14, 2022
    Posts:
    15
    Here's the original code, before I tried to use PlayerPrefs to store and reload the toggle...


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.PostProcessing;
    5. using UnityEngine.UI;
    6.  
    7. public class VignetteTutorial: MonoBehaviour
    8. {
    9.     private PostProcessVolume _postProcessVolume;
    10.     private Vignette _vignette;
    11.  
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         _postProcessVolume = GetComponent<PostProcessVolume>();
    18.         _postProcessVolume.profile.TryGetSettings(out _vignette);
    19.     }
    20.  
    21.  
    22.     public void VignetteOnOff(bool on)
    23.     {
    24.         if (on)
    25.         {
    26.             _vignette.active = true;
    27.         }
    28.         else
    29.         {
    30.             _vignette.active = false;
    31.         }
    32.     }
    33.  
    34.     public void VignetteIntensity(float sliderValue)
    35.     {
    36.         _vignette.intensity.value = sliderValue;
    37.      
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.      
    43.     }
    44. }

    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.






    PostProcessingFolder.png


    UI Toggle.png
     
    Banaaani likes this.
  7. mickstv1972

    mickstv1972

    Joined:
    Apr 14, 2022
    Posts:
    15


    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.....