コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】UnityException: LoadSerializedFileAndForget is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'XXXX'.

概要 [InitializeOnLoad] internal static class Example { static Example() { Debug.Log( ExampleSetting.instance ); } } InitializeOnLoad のタイミングで ScriptableSingleton にアクセスしたら UnityException: LoadSerializedFileAndForget is not al…

【Unity】Importer(TextScriptImporter) generated inconsistent result for asset(guid:XXXX) "YYYY"

概要 File.Move(sourceFileName, destFileName); FileUtil.MoveFileOrDirectory( source, dest ); 上記の関数でアセットのファイル名を変更したら AssetDatabase.ImportAsset のタイミングで Importer(TextScriptImporter) generated inconsistent result fo…

【Unity】Assertion failed on expression: 'it != m_SendMap.end()'

概要 Unity の Project ウィンドウで画像を選択したら Unity エディタが強制終了する現象に遭遇した Assertion failed on expression: 'it != m_SendMap.end()' ================================================================= Native Crash Reporting …

【Unity】iOS ビルドでのみ「The referenced script on this Behaviour (XXXX) is missing!」が発生した

概要 Unity エディタだと正常に動作するが iOS ビルドだと 「The referenced script on this Behaviour (XXXX) is missing!」が発生する現象に遭遇した Library フォルダを削除してからクリーンビルドしたら解決した

【Unity】Line Renderer の Order in Layer を設定してもスプライトとの描画順が変わらない場合

概要 Line Renderer が使用しているマテリアルの Shader を 「Sprites/Default」にしたら描画順が設定できるようになった

【Unity】Quad の OnPointerClick が反応しない場合

概要 using UnityEngine; using UnityEngine.EventSystems; public class Example : MonoBehaviour, IPointerClickHandler { public void OnPointerClick( PointerEventData eventData ) { Debug.Log( "OnPointerClick" ); } } 上記のようなスクリプトを Qua…

【Unity】Device Simulator だと OnPointerEnter が反応しない

概要 シーンのカメラに Physics 2D Raycaster をアタッチして シーンに Event System を配置して スプライトに OnPointerEnter 検知用のスクリプトと Circle Collider 2D をアタッチして using UnityEngine; using UnityEngine.EventSystems; public class E…

【Unity】The target object is null. Check for missing scripts.

概要 The target object is null. Check for missing scripts. Unity エディタで上記のエラーが発生する場合は 削除されたスクリプトを参照しているゲームオブジェクトが シーンに存在する可能性があります 「Missing」になっているスクリプトを右クリックし…

【Unity】SendMessage cannot be called during Awake, CheckConsistency, or OnValidate (XXXX: OnRectTransformDimensionsChange)

はじめに using UnityEngine; public class Example : MonoBehaviour { private void OnValidate() { Apply(); } private void Apply() { var rectTransform = GetComponent<RectTransform>(); rectTransform.sizeDelta = new Vector2 ( Random.Range( 0f, 100f ), Random.R</recttransform>…

【Unity】UI の Image では Material Property Block は使用できない

参考サイト様

【Unity】InvalidProgramException: Invalid IL code in XXXX:CecilAttributesGenerated_ResetStatics (): IL_0001: call 0x06000301

概要 [ResetStatic] public static bool IsInitialized { get; private set; } Cecil Attributes の Reset Static をプロパティに対して付与したら InvalidProgramException: Invalid IL code in XXXX:CecilAttributesGenerated_ResetStatics (): IL_0001: c…

【Unity】Cecil Attributes の Reset Static を null 許容値型のフィールドに使うと Unity 再生時にエディタがクラッシュする

概要 [ResetStatic] private static float? m_value; null 許容値型のフィールドに Reset Static を使ったら Unity 再生時にエディタがクラッシュする現象に遭遇した null 許容値型のフィールドに Reset Static を使わないようにしたらクラッシュしなくなっ…

【Unity】The package cache was invalidated and rebuilt because the following immutable asset(s) were unexpectedly altered:

概要 The package cache was invalidated and rebuilt because the following immutable asset(s) were unexpectedly altered: Unity で作業していたら上記の警告が出力されるようになった Unity を再起動したら出なくなった

【Unity】ビルド時に勝手に StreamingAssets フォルダが作られて UnityServicesProjectConfiguration.json というファイルが生成されるのを防ぐ方法

概要 「Version Control」パッケージが依存している 「com.unity.services.core」が勝手にフォルダやファイルを作成してしまうので Package Manager を開いて「Version Control」を選択して「Remove」する その後ビルドすると勝手に StreamingAssets フォル…

【Unity】[Package Manager Window] Cannot perform upm operation: Unable to add package [AAAA]:

概要 [Package Manager Window] Cannot perform upm operation: Unable to add package [AAAA]: Package BBBB@AAAA has invalid dependencies or related test packages: CCCC (dependency): Package [CCCC@DDDD] cannot be found [NotFound]. UnityEditor.E…

【Unity】warning: You are currently using Visual Studio 2019 to compile and link C++ code. It is recommended that you upgrade to Visual Studio 2022 for better performance.

概要 warning: You are currently using Visual Studio 2019 to compile and link C++ code. It is recommended that you upgrade to Visual Studio 2022 for better performance. Unity で PC 向けに IL2CPP ビルドしたらビルド完了時に上記の警告が出力さ…

【Unity】error: Build path contains project built with Mono scripting backend, while current project is using IL2CPP scripting backend. Consider building your project into an empty directory.

概要 error: Build path contains project built with Mono scripting backend, while current project is using IL2CPP scripting backend. Consider building your project into an empty directory. Unity で PC 向けにビルドしたら上記のエラーが発生す…

【Unity】Uncaught (in promise) DOMException: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

概要 自動再生はユーザーが承認したとき、サイト別設定で許可したとき、 またはメディアがミュートのときだけ許可されます。 Build.framework.js.gz:3:48743 Uncaught (in promise) DOMException: The play method is not allowed by the user agent or the …

【Unity】Uncaught (in promise) TypeError: e is undefined

概要 WEBGL_debug_renderer_info is deprecated in Firefox and will be removed. Please use RENDERER. Build.loader.js:1:9894 [UnityCache] Failed to load 'http://localhost:51173/Build/Build.data.br' from indexedDB cache due to the error: TypeEr…

【Unity】Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

概要 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD Unity で WebGL ビルドしたら上記のエラーが発生して Audio が再生されない現象に遭遇した 上記の記事の対応を…

【Unity】Unity 2021 以降の WebGL でゲーム開始時に Audio が再生されない場合

概要 Audio Source の Play On Awake をオンにしたり m_audioSource.Play(); スクリプトから Awake や Start のタイミングで Play 関数を呼び出した場合、 Unity エディタでは正常に Audio が再生されても WebGL でビルドすると Audio が再生されなくなりま…

【Unity】InvalidOperationException: Trying to release an object that has already been released to the pool.

概要 InvalidOperationException: Trying to release an object that has already been released to the pool. Unity のオブジェクトプールを使用した時に上記のエラーが発生する現象に遭遇した m_objectPool.Release( gameObject ); すでにプールに戻されて…

【Unity】A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.

概要 var animationDataArray = entityQuery .ToComponentDataArray<XXXX>( Allocator.TempJob ); ECS で上記のようなコードを書いたら A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details. こ</xxxx>…

【Unity】Debug.DrawLine が Game ウィンドウに描画されない場合

概要 Game ウィンドウの「Gizmos」をオンにすると描画される

【Unity】One or more cyclic dependencies detected between assemblies:

概要 One or more cyclic dependencies detected between assemblies: Packages/com.unity.2d.animation/Editor/Unity.2D.Animation.Editor.asmdef, Packages/com.unity.2d.animation/Runtime/Unity.2D.Animation.Runtime.asmdef, Packages/com.unity.2d.ani…

【Unity】Library\PackageCache\com.unity.entities@0.50.1-preview.2\Unity.Entities\DefaultWorld.cs(26,25): error CS0117: 'JobsUtility' does not contain a definition for 'ClearSystemIds'

概要 Library\PackageCache\com.unity.entities@0.50.1-preview.2\Unity.Entities\DefaultWorld.cs(26,25): error CS0117: 'JobsUtility' does not contain a definition for 'ClearSystemIds' Library\PackageCache\com.unity.entities@0.50.1-preview.2\Un…

【Unity】Exception: Lightmapping.lightingSettings is null. Please assign it to an existing asset or a new instance.

概要 Unity の「Window > Rendering > Lighting」の 「Lighting Settings Asset」が設定されていない状態で Debug.Log( Lightmapping.lightingSettings ); 上記のようなコードを記述すると Exception: Lightmapping.lightingSettings is null. Please assign…

【Unity】Method XXXX.YYYY has invalid parameters. MenuCommand is the only optional supported parameter.

概要 Method XXXX.YYYY has invalid parameters. MenuCommand is the only optional supported parameter. MenuItem が適用された関数を定義した時に上記の警告が表示される現象に遭遇した using UnityEditor; using MenuCommand = System.ComponentModel.De…

【Unity】ArgumentException: Input Button XXXX is not setup.

概要 ArgumentException: Input Button XXXX is not setup. To change the input settings use: Edit -> Settings -> Input 上記のようなエラーが発生する場合、 using UnityEngine; public class Example : MonoBehaviour { private void Update() { if ( I…

【Unity】Unable to parse Build/XXXX.framework.js.gz!

概要 Unable to parse Build/XXXX.framework.js.gz! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: gzip" present. Check …