Unityで栗を的に当てるというものを作っています。
スクリプトを書いても動きません。
Unity初心者で何が起こっているのかがよく分かりません。
プログラムをどうすると動くようになるか教えて頂きたいです。
これがIgaguriControllerのプログラムです。
using System.Collections; using System.Collections.Generic; using UnityEngine; public GameObject GetComponent; public class IgaguriController : MonoBehaviour { public void Shoot(Vector3 dir) { GetComponet<Rigidbody>().AddForce(dir); } void OnCollisionEnter(Collision other) { GetComponent<Rigidbody>().isKinematic = true; GetComponent<ParticleSystem>().Play(); } void Start() { Shoot(new Vector3(0, 200, 2000)); } }
これがIgaguriGeneratorのプログラムです。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public GameObject igaguriPrefab; void Update() { if (Input.GetMouseButtonDown(0)) { GameObject igaguri = Instantiate(igaguriPrefab) as GameObject; igaguri.GetComponent<IgaguriController>().Shoot(new Vector3(0, 200, 2000)); } } }
動かそうすると
All compiler errors have to be fixed before you can enter playmode!:
がでています。
エラーは5つ出ています。
① ReleaseAllScriptCaches did not release all script caches!
② NullReferenceException: Object reference not set to an instance of an object
UnityEditor.UIElements.UIElementsEditorUtility..cctor () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
Rethrow as TypeInitializationException: The type initializer for 'UnityEditor.UIElements.UIElementsEditorUtility' threw an exception.
UnityEditor.EditorWindow.CreateRoot () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.EditorWindow.get_rootVisualElement () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.HostView.RegisterSelectedPane (System.Boolean sendEvents) (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.HostView.OnEnable () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
③ NullReferenceException: Object reference not set to an instance of an object
UnityEditor.UIElements.UIElementsEditorUtility..cctor () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
Rethrow as TypeInitializationException: The type initializer for 'UnityEditor.UIElements.UIElementsEditorUtility' threw an exception.
UnityEditor.EditorWindow.CreateRoot () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.EditorWindow.get_rootVisualElement () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.HostView.UpdateViewMargins (UnityEditor.EditorWindow view) (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.HostView.SetWindow (UnityEditor.ContainerWindow win) (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.View.Initialize (UnityEditor.ContainerWindow win) (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.ContainerWindow.OnEnable () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
④ NullReferenceException: Object reference not set to an instance of an object
UnityEditor.UIElements.UIElementsEditorUtility..cctor () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
Rethrow as TypeInitializationException: The type initializer for 'UnityEditor.UIElements.UIElementsEditorUtility' threw an exception.
UnityEditor.EditorWindow.CreateRoot () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.EditorWindow.get_rootVisualElement () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.HostView.DeregisterSelectedPane (System.Boolean clearActualView, System.Boolean sendEvents) (at <e6e2fa0a47414cd2ac33237924761b45>:0)
UnityEditor.HostView.OnDisable () (at <e6e2fa0a47414cd2ac33237924761b45>:0)
⑤ Assets\IgaguriController.cs(5,19): error CS0116: A namespace cannot directly contain members such as fields or methods
参考にしているのは「はじめてでも安心!Unityの教科書 Unity2020完全対応版 2D&3Dスマートフォンゲーム入門講座」の本です。
(追記)
①~⑤のエラーは消えました。
次にまたエラーが出てしまいました。
修正方法を教えて頂きたいです。
エラー内容
⑥Assets\IgaguriController.cs(11,9): error CS0103: The name 'GetComponet' does not exist in the current context
⑦ Assets\IgaguriController.cs(16,22): error CS0246: The type or namespace name 'Rigibody' could not be found (are you missing a using directive or an assembly reference?)
今のIgaguriControllerのプログラム内容です。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class IgaguriController : MonoBehaviour { public void Shoot(Vector3 dir) { GetComponent<Rigidbody>().AddForce(dir); } void OnCollisionEnter(Collision other) { GetComponent<Rigibody>().isKinematic = true; GetComponent<ParticleSystem>().Play(); } void Start() { Shoot(new Vector3(0, 200, 2000)); }
回答1件
あなたの回答
tips
プレビュー