LoginSignup
3
2

More than 5 years have passed since last update.

UnityでSystem.Reflection.ReflectionTypeLoadExceptionが出た時の対処法

Posted at

結論

AssetsフォルダにあるLitJSONのAssemblyInfo.csを削除したら直りました。

原因

LitJsonはjsonライブラリです。ビルドしてLitJson.dllをUnityにインポートして使うようですが、ローカル環境のMac OSX Yosemiteではビルドできなかったのでsrc/以下のファイルをUnityのAssets以下に入れて使っていました。
その中にあるAssemblyInfo.csの

using System.Reflection;

でエラーが発生していたようです。(詳細不明)

経緯

WWW通信でJsonオブジェクトを取りたいとLitJsonをインポート、その時は特に問題なく、スクリプトを書いていたのですが、新たにシーンを作ったところ以下のエラー発生。


Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
  at System.Reflection.Assembly.GetExportedTypes () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeManager.LoadAllImportedTypes () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 

シーン内にあるゲームオブジェクトを削除してもダメ、それどころか他のシーンも同様のエラーで動かなくなっていました。

Unityのどっかがおかしくなったと思い、新たにプロジェクトを作ってAssets以下をコピーしたもののダメ。ということは問題なのはAssets以下なので、空のシーンを作り、バックアップを取ってImages,Audio,Animation...と一つ一つフォルダを消していきました。
Scriptsを消したところでエラー解消。
バックアップからScriptsを復元し再度エラーを起こし今度はScriptsの中のファイルを一つ一つ消していったところLitJsonでエラー解消。
LitJsonの公式サイトに行ったところdllが配布されていたのですが、その時たまたまネット回線が悪くDL出来なかったため自力でビルドしようとPKG_CONFIGとgmbcのパスを設定してmakeしたものの

error CS0016: Could not write to file `LitJson', cause: Could not find a part of the path "/Users/kaeruko/Desktop/honmaru/Assets/Scripts/LitJson/bin/LitJson.dll".

というエラーが出て出来ませんでした。
makefileも見たのですがLitJson.dllを生成してるところがどこかよくわからず、
LitJson.dllってビルドして出来るものじゃないの?
なんでビルド時に要求されるんだ…と思いながらLitJsonフォルダを見るとひとつだけ毛色の違うファイルを発見。

スクリーンショット 2015-06-27 11.52.24.png

消したところ動きました。
中を見ると

AssemblyInfo.cs
using System;
using System.Reflection;
using System.Runtime.CompilerServices;


[assembly: CLSCompliant (true)]

[assembly: AssemblyTitle ("LitJson")]
[assembly: AssemblyDescription ("LitJSON library")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct ("LitJSON")]
[assembly: AssemblyCopyright (
    "The authors disclaim copyright to this source code")]
[assembly: AssemblyTrademark ("")]
[assembly: AssemblyCulture ("")]

[assembly: AssemblyVersion ("@ASSEMBLY_VERSION@")]

と、エラー内容と合致していました。

はぁ、インスペクタからまたゲームオブジェクト設定しなきゃ…

3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2