- Home /
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.
In my case System.Lazy<> is working unless I try to build from Visual Studio or start Visual Studio Debugger. It simply produces compile errors because it cannot locate System.Lazy<>. Strangely it works if Unity initiates the compile.
I have resorted to emulating Lazy (http://dannykendrick.blogspot.com/2012/10/lazy-net-35.html). Lazy can be an importance performance improvement, so I hope this is resolved soon.
You can always write it yourself:
private GameObject _objectLazy;
public GameObject ObjectLazy
{
get
{
if (_objectLazy != null) return _objectLazy;
_objectLazy = new GameObject();
return _objectLazy;
}
}
From the moment you call ObjectLazy
the value is created.
Answer by nicloay · Jul 11, 2018 at 12:18 PM
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();
}
}
Your answer
Follow this Question
Related Questions
Meta data wants to be mapped elsewhere when importing assets. 0 Answers
Loading script assembly error 0 Answers
Error building Player: Exception: Failed to run assembly converter with command line -platform=wp80 0 Answers
Load and switching level problem 0 Answers
Please Help! CommandInvokationFailure and other build errors 1 Answer