実現したいこと
Buttonオブジェクトを押した結果、MainCameraの視点が切り替わるようにしたいです。
発生している問題・分からないこと
ボタン押下時に下記エラーが発生した。
エラーメッセージ
error
1ArgumentNullException: Value cannot be null. 2Parameter name: key 3System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) (at <31687ccd371e4dc6b0c23a1317cf9474>:0) 4System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <31687ccd371e4dc6b0c23a1317cf9474>:0) 5CameraManager.<Start>b__14_2 () (at Assets/Scripts/MainCamera/CameraManager.cs:88) 6UnityEngine.Events.InvokableCall.Invoke () (at <6ff3bcb667574bf9a8630184172fcfbf>:0) 7UnityEngine.Events.UnityEvent.Invoke () (at <6ff3bcb667574bf9a8630184172fcfbf>:0) 8UnityEngine.UI.Button.Press () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70) 9UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114) 10UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57) 11UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272) 12UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530) 13
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class CameraManager : MonoBehaviour 7{ 8 public static CameraManager Instance { get; private set; } 9 10 public string CurrentPositionName { get; private set; } 11 12 public GameObject ButtonLeft; 13 public GameObject ButtonRight; 14 public GameObject ButtonBack; 15 16 private class CameraPositionInfo 17 { 18 public Vector3 Position { get; set; } 19 public Vector3 Rotate { get; set; } 20 public MoveNames MoveNames { get; set; } 21 } 22 23 private class MoveNames 24 { 25 public string Left { get; set; } 26 public string Right { get; set; } 27 public string Back { get; set; } 28 } 29 30 private Dictionary<string, CameraPositionInfo> _CameraPositionInfoes = new Dictionary<string, CameraPositionInfo> 31 { 32 { 33 "Door", 34 new CameraPositionInfo 35 { 36 Position = new Vector3(715,150,530), 37 Rotate = new Vector3(0,0,0), 38 MoveNames = new MoveNames 39 { 40 Left = "RoomLeft", 41 Right = "RoomRight" 42 } 43 } 44 }, 45 { 46 "RoomRight", 47 new CameraPositionInfo 48 { 49 Position = new Vector3(715,150,530), 50 Rotate = new Vector3(0,72,0), 51 MoveNames = new MoveNames 52 { 53 Left = "Door", 54 } 55 } 56 }, 57 { 58 "RoomLeft", 59 new CameraPositionInfo 60 { 61 Position = new Vector3(715,150,530), 62 Rotate = new Vector3(0,288,0), 63 MoveNames = new MoveNames 64 { 65 Left = "Door", 66 } 67 } 68 }, 69 70 }; 71 72 // Start is called before the first frame update 73 void Start() 74 { 75 Instance = this; 76 ChangeCameraPosition("Door"); 77 78 ButtonBack.GetComponent<Button>().onClick.AddListener(() => 79 { 80 ChangeCameraPosition(_CameraPositionInfoes[CurrentPositionName].MoveNames.Back); 81 }); 82 ButtonLeft.GetComponent<Button>().onClick.AddListener(() => 83 { 84 ChangeCameraPosition(_CameraPositionInfoes[CurrentPositionName].MoveNames.Left); 85 }); 86 ButtonRight.GetComponent<Button>().onClick.AddListener(() => 87 { 88 ChangeCameraPosition(_CameraPositionInfoes[CurrentPositionName].MoveNames.Right); 89 }); 90 91 } 92 93 public void ChangeCameraPosition(string positionName) 94 { 95 if (CurrentPositionName == null) return; 96 CurrentPositionName = positionName; 97 GetComponent<Camera>().transform.position = _CameraPositionInfoes[CurrentPositionName].Position; 98 GetComponent<Camera>().transform.rotation = Quaternion.Euler(_CameraPositionInfoes[CurrentPositionName].Rotate); 99 } 100 101}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Googleで該当のエラーを検索したが、同条件のエラー解消が見つからなかった。
補足
Unity (2022.3.50f1) LTS
あなたの回答
tips
プレビュー