Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by erick-fda · May 22, 2017 at 03:19 PM · unity 5error messageprotection

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aidesigner · Jul 02, 2017 at 04:13 PM 0
Share

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.

avatar image ShadyProductions aidesigner · Jul 02, 2017 at 05:39 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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();
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges