シリアル化は、データ構造やオブジェクトの状態を、Unity が保存可能で後で再構築できる形式に変換する自動プロセスです。詳しい情報は スクリプトのシリアル化 を参照してください。
特定の状況では、スクリプトのシリアル化によってエラーが発生する可能性があります。これらのうちのいくつかに対する修正方を以下に説明します。
MonoBehaviour コンストラクターやフィールドイニシャライザーの中で GameObject.Find などのスクリプティング API を呼び出すと、「Find is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call in in Awake or Start instead.MonoBehaviour」(コンストラクターまたはインスタンスフィールドイニシャライザーから Find を呼び出すことはできません。代わりに Awake または Start 内で呼び出してください) というエラーが発生します。
これを修正するには、コンストラクターではなく MonoBehaviour.Start 内でスクリプティング API を呼び出します。
System.Serializable
とマークされたクラスのコンストラクター内から GameObject.Find などのスクリプティング API を呼び出すと、「Find is not allowed to be called during serialization, call it from Awake or Start instead」(シリアル化中は Find を呼び出すことはできません。代わりに Awake または Start 内で呼び出してください) というエラーが発生します。
これを修正するには、シリアライズされたオブジェクトに関するスクリプティング API の呼び出しが、コンストラクターで行われないようにコードを変更します。
スクリプティング API の多くは、上記の制限の影響を受けます。Unity スクリプティング API の一部のみがその例外で、どこからでも呼び出すことができます。該当する API は以下の通りです。
Mathf 関数
単純な自己完結型の構造体。例えば、Vector3 や Quaternion のような数学ライブラリで使用される構造体など。
シリアル化中のエラー発生のリスクを減らすために、自己完結型で、Unity からデータを取得または設定する必要がない API メソッドのみを呼び出してください。他に選択肢がない場合にのみこれらを呼び出すようにしてください。
• 2017–05–15 公開ページ
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.