実現したいこと
プレハブアセットを、スクリプトで取得したい。
前提
プレハブについて学んでいるのですが、本に書いてある通りに書いてもエラーが発生するので、質問を投稿させていただきました。
発生している問題・エラーメッセージ
エラーメッセージ ```UnityException: OpenPreviewScene is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'prefabscript' on game object 'Cube'. See "Script Serialization" page in the Unity Manual for further details. UnityEditor.SceneManagement.EditorSceneManager.OpenPreviewScene (System.String scenePath, System.Boolean allocateSceneCullingMask) (at <6a5b55f2e18b419e9faedac06ac6af94>:0) UnityEditor.PrefabUtility.LoadPrefabContents (System.String assetPath) (at <6a5b55f2e18b419e9faedac06ac6af94>:0) prefabscript..ctor () (at Assets/prefabscript.cs:7) UnityEditorInternal.InternalEditorUtility:HierarchyWindowDragByID(Int32, HierarchyDropFlags, Transform, Boolean) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) ### 該当のソースコード ```ここに言語名を入力 C# ソースコード ```using UnityEditor; using UnityEngine; public class prefabscript : MonoBehaviour { GameObject prefab = PrefabUtility.LoadPrefabContents("Assets/BaseObject.prefab"); // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } } ### 試したこと プレハブアセットでないものを書いたり、いったんオブジェクト変数にいれてそれを()内に入れたりもしましたがだめでした。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。