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

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

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

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

Q&A

解決済

1回答

2499閲覧

unityの教科書にて実行できるのにコード通りに動きません

sktia1

総合スコア9

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

0グッド

0クリップ

投稿2020/08/17 04:31

編集2020/08/17 06:43
猫のオブジェクトをspaceを押した時にジャンプさせるために下のコードを作成し,コンパイルエラーなく実行できたのですがspaceを押してもジャンプせず、下のエラーコードが表示されます。
調べたこと エラーコードを検索にかけて、英語のものをGoogle翻訳しましたが翻訳内容の指示は理解できませんでした。

C#

1コード 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class PlayerController : MonoBehaviour 7{ 8 Rigidbody2D rigid2D; 9 float jumpForce = 780.0f; 10 // Start is called before the first frame update 11 void Start() 12 { 13 this.rigid2D = GetComponent<Rigidbody2D>(); 14 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 if(Input.GetKeyDown(KeyCode.Space)){ 21 this.rigid2D.AddForce(transform.up*this.jumpForce); 22 } 23 } 24} 25
エラーコード ArgumentException: Getting control 0's position in a group with only 0 controls when doing KeyDown Aborting UnityEngine.GUILayoutGroup.GetNext () (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/LayoutGroup.cs:122) UnityEngine.GUILayoutUtility.BeginLayoutGroup (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options, System.Type layoutType) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUILayoutUtility.cs:309) UnityEngine.GUILayout.BeginHorizontal (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUILayout.cs:259) UnityEngine.GUILayout.BeginHorizontal (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUILayout.cs:251) UnityEditor.SceneView.DoToolbarGUI () (at /Users/builduser/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:1279) UnityEditor.SceneView.OnGUI () (at /Users/builduser/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:2376) System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <fb001e01371b4adca20013e0ac763896>:0) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <fb001e01371b4adca20013e0ac763896>:0) UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:381) UnityEditor.HostView.Invoke (System.String methodName) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:375) UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:351) UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at /Users/builduser/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:406) UnityEditor.DockArea.OldOnGUI () (at /Users/builduser/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:395) UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:293) UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:508) UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:491) UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Boolean canAffectFocus) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:484) UnityEngine.UIElements.IMGUIContainer.SendEventToIMGUI (UnityEngine.UIElements.EventBase evt, System.Boolean canAffectFocus) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:477) UnityEngine.UIElements.IMGUIContainer.HandleEvent (UnityEngine.UIElements.EventBase evt) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:449) UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/Events/IEventDispatchingStrategy.cs:81) UnityEngine.UIElements.MouseEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/Events/MouseEventDispatchingStrategy.cs:69) UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:310) UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:275) UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:238) UnityEngine.UIElements.EventDispatcher.OpenGate () (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:203) UnityEngine.UIElements.EventDispatcherGate.Dispose () (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:44) UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:301) UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/EventDispatcher.cs:159) UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/Panel.cs:257) UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/UIElementsUtility.bindings.cs:404) UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/UIElementsUtility.bindings.cs:194) UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:197)

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

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

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

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

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

m.ts10806

2020/08/17 05:00

>英語のものしか出てこず、理解できませんでした それは単に英語だから最初から読まなかったのか、Google翻訳にかけても分からなかったのか、どちらでしょうか。質問本文にきちんと「調べたこと試したこと」を具体的に記載してください。 あと、コードやエラーはマークダウンのcode機能にてご提示ください。
sktia1

2020/08/17 06:10

ご指摘ありがとうございます。 確かにgoogle翻訳にはかけていませんでした。教えていただく側として最低限やるべきこともやらずに質問して申し訳ありませんでした。 また、マークダウンで記述しました。まだ至らぬ点があると思いますが、ご指摘していただけると幸いです。
m.ts10806

2020/08/17 06:14 編集

エラーの方もマークダウンのcodeでお願いします。 ``` ``` バッククォート1つだけ使う場合は1行のコードとかコード内引用とかが適当です。複数行は3つで。 https://teratail.com/help Unityに詳しいわけではないですが、 1行目の:Getting control 0's position in a group with only 0 controls when doing KeyDown これだけでも結構なヒントになりそうですが、そうでもないですか? https://qiita.com/cannorin/items/eb062aae88bfe2ad6fe5
sktia1

2020/08/17 07:22

エラーコード訂正しました。 グーグル翻訳で1行目のエラーコードと英語の同じような質問がされていたサイトを翻訳したところ エラーコードの翻訳:KeyDownを実行するときにコントロールが0しかないグループでコントロール0の位置を取得する 英語の回答の翻訳:実際のイベントが処理されるとき、OnGUIがLayoutイベントが呼び出されたときと同じ内容であることが重要です。そのため、GUIの状態を変更するロジックをOnGUI内に配置しないでください。 OnGUIに配置する場合は、ラップしてください と翻訳されコントロールを調べたところGUIについて出てきたためGUIに問題がありそうというところまではわかりましたが、どうすれば良いのか見当がつかないです。
m.ts10806

2020/08/17 07:54 編集

「コントロールが0しかない」だから、操作しようとしているコントロールがそもそも設置されてないとかでは?
sktia1

2020/08/17 08:11

なるほど!スクリプトをアタッチするのを忘れていました、おかげさまで解決しました。 質問の仕方についても丁寧に教えていただきありがとうございました。 以後気をつけます
sktia1

2020/08/17 08:19

1から10までありがとうございます。 以後エラーの読み方の勉強と質問する前に検索は徹底するようにします。 せめての気持ちとしてベストアンサーにさせていただきたいのですがいかがでしょうか?
m.ts10806

2020/08/17 08:20

なるほど。了解です。 では簡単ですが回答におこしてみようと思います。
guest

回答1

0

ベストアンサー

Unity自体は詳しくはないのですが、エラーからなんとなく汲み取れそうに思います。

Getting control 0's position in a group with only 0 controls when doing KeyDown

Google翻訳:KeyDownを実行するときにコントロールが0しかないグループでコントロール0の位置を取得する

この内容からすると「コントロールが0しかない」=「コントロールが設置されていない」と解釈することもできます。
そもそも対象のコントロールがGUIに設置されていない可能性があります。

プログラミングの基本的な手順としては「定義したものを使う」ので手順を見直すとこの手のミスは減らせるかもしれません。

エラーメッセージの読み解き方は下記のような記事を参考にしてください。
エラーメッセージの読み方と対処, 検索や質問の原則

投稿2020/08/17 08:42

m.ts10806

総合スコア80850

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問