Unity 5.6.1 Causes Error When Using System.Lazy

I recently tried updating Unity to version 5.6.1 from version 5.6.0.

After updating, I opened a project which I had been working on and was error-free with the previous version. I got an error in the Unity console as below:

Assets/Scripts/Singletons/Singleton.cs(32,26): error CS0122: `System.Lazy<T>' is inaccessible due to its protection level

In Visual Studio Code, which I use for script editing, I got a matching error:

'Lazy<T>' is inaccessible due to its protection level [Assembly-CSharp]

The error occurred on the line of code below, where I declare a field of type System.Lazy<>:

private static readonly Lazy<T> _instance = new Lazy<T>(() => CreateInstance());

I’ve been using the System.Lazy<T> class since the start of the project and have had no problems with it until this update to version 5.6.1. To verify that this was actually the cause of the problem, I uninstalled Unity and reverted to the previous version, 5.6.0, again. The error disappeared and I was able to run the game without a problem. Then I uninstalled again and reinstalled the new version, 5.6.1, and the error returned.

Finally, to remove all other factors, I created a new empty project using the new Unity version, 5.6.1, and created a single script containing only this:

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

public class Test : MonoBehaviour
{
    public Lazy<MonoBehaviour> myLazy;
}

This produced the same error as the line of code I mentioned above.

I’m wondering whether this is something that should be a bug report, but I thought it was worth it to check here in case someone has encountered something like this before and there is a known fix. Has anyone else encountered similar problems with either this Unity version update or a previous one, and if so, how was the problem resolved (if it was)?

For now, I’ve gone back to version 5.6.0 and have not experienced the error since.

Just change scripting runtime version to 4.6 if you want to support Lazy. as it was introduced only in NET v4
You can also use this simple implementation if you don’t care about all lazy features like thread safe, etc.

/// <summary>
/// Provides support for lazy initialization.
/// </summary>
/// <typeparam name="T">Specifies the type of object that is being lazily initialized.</typeparam>
public sealed class Lazy<T>
{
    private readonly object padlock = new object();
    private readonly Func<T> createValue;
    private bool isValueCreated;
    private T value;

    /// <summary>
    /// Gets the lazily initialized value of the current Lazy{T} instance.
    /// </summary>
    public T Value
    {
        get
        {
            if (!isValueCreated)
            {
                lock (padlock)
                {
                    if (!isValueCreated)
                    {
                        value = createValue();
                        isValueCreated = true;
                    }
                }
            }
            return value;
        }
    }

    /// <summary>
    /// Gets a value that indicates whether a value has been created for this Lazy{T} instance.
    /// </summary>
    public bool IsValueCreated
    {
        get
        {
            lock (padlock)
            {
                return isValueCreated;
            }
        }
    }


    /// <summary>
    /// Initializes a new instance of the Lazy{T} class.
    /// </summary>
    /// <param name="createValue">The delegate that produces the value when it is needed.</param>
    public Lazy(Func<T> createValue)
    {
        if (createValue == null) throw new ArgumentNullException("createValue");

        this.createValue = createValue;
    }


    /// <summary>
    /// Creates and returns a string representation of the Lazy{T}.Value.
    /// </summary>
    /// <returns>The string representation of the Lazy{T}.Value property.</returns>
    public override string ToString()
    {
        return Value.ToString();
    }
}