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

The type initializer for 'MyClass' threw an exception

Discussion in 'Scripting' started by Jorhoto, Jul 16, 2020.

  1. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    99
    Hello!
    I am finding the error message on the title when compiling for Android with IL2CPP.
    *It works ok in editor and on Android if I select Mono as scripting backend instead of IL2CPP.

    Inner Exception: linked away
    Exception: The type initializer for 'MyClass' threw an exception.

    I have something like this in code:
    Code (CSharp):
    1. public class MyClass : MyBaseClass
    2. // singleton
    3. public static readonly MyClass Instance = new MyClass();
    4.  
    5. // private constructor
    6. private MyClass()
    7. {
    8.    // Do things
    9. }
    Thanks!
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    Does the code above throw the exception? If not, can you post the full code where you are having the problem (minimum repro case)?
     
  3. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    99
    Hi, I just submitted a bug to Unity.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,573
    What is "MyBaseClass"? Are you sure you **didn't** derive your MyBaseClass class from MonoBehaviour or ScriptableObject?

    Since you initialize an instance in your static field initializers you essentially call your contructor during your type initialization. Since you stripped all your code from your constructor, how can we tell what's the issue?
     
  5. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    99
    After checking the code, the problem wasn't with the static constructor itself; but with what I was doing in it.

    The issue happens when you create a derived class from System.Data.DataTable and compile for Android under IL2CPP.

    I created a fresh new project (2019.4.5f1) where the crash happens always (at least in my system). In case anyone is interested:
    https://drive.google.com/file/d/1Z9K_JMUpBtmxzU74LDhQUnxLHLtz8IRi/view?usp=sharing