質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

4614閲覧

エラーの直し方が分かりません。どうしたら動くか教えて欲しいです。

s_1217

総合スコア1

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/03/28 03:24

編集2021/03/28 08:47

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)); }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2021/03/28 03:28

エラーが出るならエラーメッセージを提示しましょう いらぬ省略翻訳しないで、出たそのままをコピペで提示しましょう
fiveHundred

2021/03/28 03:31

public GameObject GetComponent; 上記が意味不明すぎますが、何のために書いたのでしょうか?
s_1217

2021/03/28 03:36

違うエラーが出た時に調べたら解決策として出てきたので書いてみました
fiveHundred

2021/03/28 03:42

どういう解決策なのか分かりませんが、以下の理由から、解決どころか、むしろエラーを増やしています。 - このような定義はclassの中でしか出来ないため、文法エラーになります - GetComponentはすでに関数として存在し、それを空の変数に上書きすることになるため、それが正しく動作しなくなります
s_1217

2021/03/28 03:46

先程の1行を削除しました。 しかし、①~④のエラーがなおりません。
fiveHundred

2021/03/28 03:52

他のプロジェクトで同様のエラーは出ますか?
guest

回答1

0

ベストアンサー

① NullReferenceException

Unityを再起動してみてください。

参考:https://raspberly.hateblo.jp/entry/UIElementsError

A namespace cannot directly contain members such as fields or methods

IgaguriGenerator

cs

1public class NewBehaviourScript : MonoBehaviour 23public class IgaguriGenerator : MonoBehaviour

IgaguriController

cs

1GetComponet<Rigidbody>().AddForce(dir); 23GetComponent<Rigidbody>().AddForce(dir);

に変更してみてはどうですか?

(追記)

cs

1GetComponent<Rigibody>().isKinematic = true; 23GetComponent<Rigidbody>().isKinematic = true;

RigidbodyがRigibodyになっています

投稿2021/03/28 03:30

編集2021/03/28 09:19
coolwind0202

総合スコア708

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

s_1217

2021/03/28 03:36

ありがとうございます。 最後のエラーがなおりました。
coolwind0202

2021/03/28 03:41

最後のエラーということは、NullReferenceException は消えていないんですね。 IgaguriControllerの public GameObject GetComponent; この行を削除するとどうなるでしょうか?
s_1217

2021/03/28 03:43

削除しても変わりませんでした
coolwind0202

2021/03/28 04:14 編集

すでに書いていますが、IgaguriController.cs内に GetComponetになっている箇所があるのでGetComponentに修正してください。 見落としていましたが、こちらの回答でRigidbodyがRigibodyになっている箇所がありました、すみません。
s_1217

2021/03/28 04:20

ありがとうございます ⑥が直りました。
coolwind0202

2021/03/28 04:30

⑦も誤字が原因のように思うのですが、 IgaguriController.csが今どうなっているか、全文を追記していただけませんか?
s_1217

2021/03/28 08:48

遅くなりましたが、追記させて頂きました
coolwind0202

2021/03/28 09:25

エラーの原因を追う方法として、多くの場合行数を見て探すのが楽です。 Assets\IgaguriController.cs(16,22): error CS0246: The type or namespace name 'Rigibody' could not be found 最初の方にある (16,22) の 16 は IgaguriController.cs の16行目でエラーが起きている、ということなので、16行目付近の処理とエラーの文章を照らし合わせて探します。 エラー文を機械翻訳にかけるだけでも分かりやすくなります。
s_1217

2021/03/28 13:45

ありがとうございます。 無事できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問