Search code examples
c#unity-game-enginepost-processing

Why am I getting error on TryGetSettings when using postprocessing v2 ? And how can I fix it?


In the editor on my Camera I added two components : Post Process Layer and Post Process Volume On the first component the layer I changed the Layer to a layer I added PostProcessing On the second component I set the Is Global to true and added one effect for now Depth Of Field

Now I want to change and set values to the Depth Of Field Focal Length via a script. So I created a new script :

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

public class PostprocessingEffects : MonoBehaviour
{
    private PostProcessVolume postProcessVolume;
    private DepthOfField depthOfField;

    void Start()
    {
        postProcessVolume = GetComponent<PostProcessVolume>();
        postProcessVolume.profile.TryGetSettings(out PostProcessEffectSettings depthOfFieldSettings);

        depthOfField.focalLength.value = 1f;
    }
}

I'm getting error on the line :

postProcessVolume.profile.TryGetSettings(out depthOfField);

On the TryGetSettings and the error is :

The type 'PostprocessingEffects' cannot be used as type parameter 'T' in the generic type or method 'PostProcessProfile.TryGetSettings(out T)'. There is no implicit reference conversion from 'PostprocessingEffects' to 'UnityEngine.Rendering.PostProcessing.PostProcessEffectSettings'.

Tried also :

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

public class PostprocessingEffects : MonoBehaviour
{
    private PostProcessVolume postProcessVolume;
    private DepthOfField depthOfField;

    void Start()
    {
        postProcessVolume = GetComponent<PostProcessVolume>();
        postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);

        depthOfField.focalLength.value = 1f;
    }
}

Solution

  • It seems you're using incorrect type.

    TryGetSettings cannot take type of PostprocessingEffects I believe you want DepthOfField try:

    postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);
    

    of if you have your variable already defined:

    private DepthOfField depthOfField;
    postProcessVolume.profile.TryGetSettings(out depthOfField);
    

    Also make sure you have depth of field effect attached to Postprocess Volume in inspector