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

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

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

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

Unity

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

Q&A

解決済

3回答

1838閲覧

修飾子がアイテムに対して有効にならない

igu..

総合スコア1

C#

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

Unity

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

0グッド

0クリップ

投稿2023/01/24 15:03

編集2023/01/24 15:05

前提

こちらのサイトを見ながら2Dアクションを作っています。
https://dkrevel.com/makegame-beginner/make-2d-action-script-tidy/
このサイトを見ながら今まで書いたプログラムを整理していたのですが、このサイト通りに整理したら今までは問題なく動いていたのにエラーが発生するようになりました、どうすればよいでしょうか?

発生している問題・エラーメッセージ

Assets\player.cs(60,9): error CS0106: The modifier 'private' is not valid for this item Assets\player.cs(111,9): error CS0106: The modifier 'private' is not valid for this item Assets\player.cs(157,9): error CS0106: The modifier 'private' is not valid for this item

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class player : MonoBehaviour 6{ 7 private Rigidbody2D myRigidBody; 8 private bool isGround=false; 9 private Animator anim=null; 10 private Rigidbody2D rb=null; 11 private bool isjump=false; 12 private bool isHead=false; 13 private float jumpTime=0.0f; 14 private float jumpPos=0.0f; 15 private float dashTime=0.0f; 16 private float beforeKey=0.0f; 17 private bool isRun=false; 18 19 20 [Header("設置判定")]public GroundCheck ground; 21 [Header("頭をぶつけた判定")]public GroundCheck head; 22 [Header("移動速度")]public float speed; 23 [Header("重力")]public float gravity; 24 [Header("ジャンプ速度")]public float jumpSpeed; 25 [Header("ジャンプする高さ")]public float jumpHeight; 26 [Header("ジャンプ制限時間")]public float jumpLimitTime; 27 [Header("ダッシュの速さ表現")]public AnimationCurve dashCurve; 28 [Header("ジャンプの速さ表現")]public AnimationCurve jumpCurve; 29 30 31 32 33 void Start() 34 { 35 anim=GetComponent<Animator>(); 36 rb=GetComponent<Rigidbody2D>(); 37 } 38 39 40 void Update() 41 { 42 //設置判定を得る 43 isGround=ground.IsGround(); 44 isHead=head.IsGround(); 45 46 //各種座標軸の速度を求める 47 float xSpeed=GetXSpeed(); 48 float ySpeed=GetYSpeed(); 49 50 //アニメーションを適用 51 SetAnimation(); 52 53 //移動速度を設定 54 rb.velocity=new Vector2(xSpeed,ySpeed); 55 56 /// <summary> 57 /// Y成分で必要な計算をし、速度を返す 58 /// </summary> 59 /// <returns>Y軸の速さ</returns> 60 private float GetYSpeed() 61 { 62 float verticalKey=Input.GetAxisRaw("Vertical"); 63 float ySpeed=-gravity; 64 65 if(isGround && Input.GetButtonDown("Vertical")) 66 { 67 if(verticalKey>0) 68 { 69 ySpeed=jumpSpeed; 70 jumpPos=transform.position.y;//ジャンプした位置を記録する 71 isjump=true; 72 jumpTime=0.0f; 73 74 }else 75 { 76 isjump=false; 77 } 78 } 79 else if(isjump) 80 { 81 //spaceキーを押しているか 82 bool pushUpKey=verticalKey>0; 83 //現在の高さが飛べる高さより下か 84 bool canHeight=jumpPos+jumpHeight>transform.position.y; 85 //ジャンプ時間が長くなりすぎてないか 86 bool canTime=jumpLimitTime>jumpTime; 87 if(pushUpKey && canHeight && canTime &&! isHead) 88 { 89 ySpeed=jumpSpeed; 90 jumpTime+=Time.deltaTime; 91 } 92 else 93 { 94 isjump=false; 95 jumpTime=0.0f; 96 } 97 } 98 //アニメーションカーブを速度に適用 99 if(isjump) 100 { 101 ySpeed*=jumpCurve.Evaluate(jumpTime); 102 } 103 104 return ySpeed; 105 } 106 107 /// <summary> 108 /// X成分せ必要な計算をし、速度を返す 109 /// </summary> 110 /// <returns>X軸の速さ</returns> 111 private float GetXSpeed() 112 { 113 float horizontalKey=Input.GetAxisRaw("Horizontal"); 114 float xSpeed=0.0f; 115 116 if(horizontalKey>0) 117 { 118 transform.localScale=new Vector3(1,1,1); 119 isRun=true; 120 dashTime+=Time.deltaTime; 121 xSpeed=-speed; 122 } 123 else if(horizontalKey<0) 124 { 125 transform.localScale=new Vector3(-1,1,1); 126 isRun=true; 127 dashTime+=Time.deltaTime; 128 xSpeed=-speed; 129 } 130 else 131 { 132 isRun=false; 133 dashTime=0.0f; 134 xSpeed=0.0f; 135 } 136 137 //前回の入力からダッシュの反転を判断して速度を変える 138 if(horizontalKey>0 && beforeKey<0) 139 { 140 dashTime=0.0f; 141 } 142 else if(horizontalKey<0 && beforeKey>0) 143 { 144 dashTime=0.0f; 145 } 146 beforeKey=horizontalKey; 147 148 //アニメーションカーブを適用 149 xSpeed*=dashCurve.Evaluate(dashTime); 150 151 return xSpeed; 152 } 153 154 /// <summary> 155 /// アニメーションを設定する 156 /// </summary> 157 private void SetAnimation() 158 { 159 anim.SetBool("jumo",isjump); 160 anim.SetBool("ground",isGround); 161 anim.SetBool("run",isRun); 162 } 163 164 } 165} 166 167}

補足情報(FW/ツールのバージョンなど)

参考サイトでは62行目と113行目は
float verticalKey=Input.GetAxis("Vertical"); , float horizontalKey=Input.GetAxis("Horizontal");
こうなっていますが私は
float verticalKey=Input.GetAxisRaw("Vertical"); , float horizontalKey=Input.GetAxisRaw("Horizontal");
にしています、参考サイトコードだと一度ボタンを押しただけで二度飛んでしまったりとおかしな挙動になったので変えています
65行目も参考サイトでは
if(isGround)
になっていますが
if(isGround && Input.GetButtonDown("Vertical"))
に変更しています、ジャンプボタン長押しでもジャンプを一度だけにするために変更しています。
それ以外はサイトに搭載されているコードと一緒です。

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2023/01/24 23:56

質問する前にエラーメッセージでググって調べるぐらいのことはしたのですか? 読んでみましょう⇒ https://teratail.com/help/question-tips
igu..

2023/01/25 01:00

はい、エラーメッセージも調べ4時間ほど自分で何とかしようと調べていましたが何も分からず諦めかけていたところここに辿り着きました。自分の調べ方が下手だったとは思います
guest

回答3

0

回答としてはUnchFullburstさんが答えている内容に他なりません。
補足しますと、参考元のサイトのスクリプトではUpdateメソッドとGetYSpeedメソッドははっきりスコープが分かれています。

一方で質問者であるigu..さんのスクリプトを見ると、Upadetメソッド内でGetYSpeedメソッドを宣言しているので、エラーが発生しています。C#のコーディングの規則上、このような書き方はできません。
なので、Classスコープ直下に書きましょう。ついでに言うと、提示されているスクリプトは一つ}が多いです。

このような事が発生する原因として、スクリプトのインデントが正しくできていない事が原因です。可読性を上げるためにもインデントは正しく行いましょう。

以上を加味して修正したスクリプトを3番目のスクリプトとして記載しておきます。

1,参考元スクリプトの一部抜粋

C#

1 2 void FixedUpdate() 3 { 4 //接地判定を得る 5 isGround = ground.IsGround(); 6 isHead = head.IsGround(); 7 8 //各種座標軸の速度を求める 9 float xSpeed = GetXSpeed(); 10 float ySpeed = GetYSpeed(); 11 12 //アニメーションを適用 13 SetAnimation(); 14 15 //移動速度を設定 16 rb.velocity = new Vector2(xSpeed, ySpeed); 17 } 18 19 -------------------------------------------------- 20 21 private float GetYSpeed() 22 { 23 float verticalKey = Input.GetAxis("Vertical"); 24 float ySpeed = -gravity; 25 26 if (isGround) 27 { 28 if (verticalKey > 0) 29 { 30 ySpeed = jumpSpeed; 31 jumpPos = transform.position.y; //ジャンプした位置を記録する 32 isJump = true; 33 jumpTime = 0.0f; 34 } 35 else 36 { 37 isJump = false; 38 } 39 } 40 else if (isJump) 41 { 42 //上方向キーを押しているか 43       bool pushUpKey = verticalKey > 0; 44       //現在の高さが飛べる高さより下か 45       bool canHeight = jumpPos + jumpHeight > transform.position.y; 46       //ジャンプ時間が長くなりすぎてないか 47       bool canTime = jumpLimitTime > jumpTime; 48 49       if (pushUpKey && canHeight && canTime && !isHead) 50 { 51 ySpeed = jumpSpeed; 52 jumpTime += Time.deltaTime; 53 } 54 else 55 { 56 isJump = false; 57 jumpTime = 0.0f; 58 } 59 } 60 61 if (isJump) 62 { 63 ySpeed *= jumpCurve.Evaluate(jumpTime); 64 } 65 66 return ySpeed; 67 }

2,質問者さんの提示スクリプト

C#

1 void Update() 2 { 3 //設置判定を得る 4 isGround=ground.IsGround(); 5 isHead=head.IsGround(); 6 7 //各種座標軸の速度を求める 8 float xSpeed=GetXSpeed(); 9 float ySpeed=GetYSpeed(); 10 11 //アニメーションを適用 12 SetAnimation(); 13 14 //移動速度を設定 15 rb.velocity=new Vector2(xSpeed,ySpeed); 16 17 /// Updateメソッド内 18 private float GetYSpeed() 19 { 20 float verticalKey=Input.GetAxisRaw("Vertical"); 21 float ySpeed=-gravity; 22 23 if(isGround && Input.GetButtonDown("Vertical")) 24 { 25 if(verticalKey>0) 26 { 27 ySpeed=jumpSpeed; 28 jumpPos=transform.position.y;//ジャンプした位置を記録する 29 isjump=true; 30 jumpTime=0.0f; 31 32 }else 33 { 34 isjump=false; 35 } 36 } 37 else if(isjump) 38 { 39 //spaceキーを押しているか 40 bool pushUpKey=verticalKey>0; 41 //現在の高さが飛べる高さより下か 42 bool canHeight=jumpPos+jumpHeight>transform.position.y; 43 //ジャンプ時間が長くなりすぎてないか 44 bool canTime=jumpLimitTime>jumpTime; 45 if(pushUpKey && canHeight && canTime &&! isHead) 46 { 47 ySpeed=jumpSpeed; 48 jumpTime+=Time.deltaTime; 49 } 50 else 51 { 52 isjump=false; 53 jumpTime=0.0f; 54 } 55 } 56 //アニメーションカーブを速度に適用 57 if(isjump) 58 { 59 ySpeed*=jumpCurve.Evaluate(jumpTime); 60 } 61 62 return ySpeed; 63 } 64 //以下省略 65 } 66

3,修正したスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class player : MonoBehaviour 6{ 7 private Rigidbody2D myRigidBody; 8 private bool isGround=false; 9 private Animator anim=null; 10 private Rigidbody2D rb=null; 11 private bool isjump=false; 12 private bool isHead=false; 13 private float jumpTime=0.0f; 14 private float jumpPos=0.0f; 15 private float dashTime=0.0f; 16 private float beforeKey=0.0f; 17 private bool isRun=false; 18 19 20 [Header("設置判定")]public GroundCheck ground; 21 [Header("頭をぶつけた判定")]public GroundCheck head; 22 [Header("移動速度")]public float speed; 23 [Header("重力")]public float gravity; 24 [Header("ジャンプ速度")]public float jumpSpeed; 25 [Header("ジャンプする高さ")]public float jumpHeight; 26 [Header("ジャンプ制限時間")]public float jumpLimitTime; 27 [Header("ダッシュの速さ表現")]public AnimationCurve dashCurve; 28 [Header("ジャンプの速さ表現")]public AnimationCurve jumpCurve; 29 30 31 void Start() 32 { 33 anim=GetComponent<Animator>(); 34 rb=GetComponent<Rigidbody2D>(); 35 } 36 37 38 void Update() 39 { 40 //設置判定を得る 41 isGround=ground.IsGround(); 42 isHead=head.IsGround(); 43 44 //各種座標軸の速度を求める 45 float xSpeed=GetXSpeed(); 46 float ySpeed=GetYSpeed(); 47 48 //アニメーションを適用 49 SetAnimation(); 50 51 //移動速度を設定 52 rb.velocity=new Vector2(xSpeed,ySpeed); 53 } 54 55 /// <summary> 56 /// Y成分で必要な計算をし、速度を返す 57 /// </summary> 58 /// <returns>Y軸の速さ</returns> 59 private float GetYSpeed() 60 { 61 float verticalKey=Input.GetAxisRaw("Vertical"); 62 float ySpeed=-gravity; 63 64 if(isGround && Input.GetButtonDown("Vertical")) 65 { 66 if(verticalKey>0) 67 { 68 ySpeed=jumpSpeed; 69 jumpPos=transform.position.y;//ジャンプした位置を記録する 70 isjump=true; 71 jumpTime=0.0f; 72 73 } 74 else 75 { 76 isjump=false; 77 } 78 } 79 else if(isjump) 80 { 81 //spaceキーを押しているか 82 bool pushUpKey=verticalKey>0; 83 //現在の高さが飛べる高さより下か 84 bool canHeight=jumpPos+jumpHeight>transform.position.y; 85 //ジャンプ時間が長くなりすぎてないか 86 bool canTime=jumpLimitTime>jumpTime; 87 88 if(pushUpKey && canHeight && canTime &&! isHead) 89 { 90 ySpeed=jumpSpeed; 91 jumpTime+=Time.deltaTime; 92 } 93 else 94 { 95 isjump=false; 96 jumpTime=0.0f; 97 } 98 } 99 //アニメーションカーブを速度に適用 100 if(isjump) 101 { 102 ySpeed*=jumpCurve.Evaluate(jumpTime); 103 } 104 105 return ySpeed; 106 } 107 108 /// <summary> 109 /// X成分せ必要な計算をし、速度を返す 110 /// </summary> 111 /// <returns>X軸の速さ</returns> 112 private float GetXSpeed() 113 { 114 float horizontalKey=Input.GetAxisRaw("Horizontal"); 115 float xSpeed=0.0f; 116 117 if(horizontalKey>0) 118 { 119 transform.localScale=new Vector3(1,1,1); 120 isRun=true; 121 dashTime+=Time.deltaTime; 122 xSpeed=-speed; 123 } 124 else if(horizontalKey<0) 125 { 126 transform.localScale=new Vector3(-1,1,1); 127 isRun=true; 128 dashTime+=Time.deltaTime; 129 xSpeed=-speed; 130 } 131 else 132 { 133 isRun=false; 134 dashTime=0.0f; 135 xSpeed=0.0f; 136 } 137 138 //前回の入力からダッシュの反転を判断して速度を変える 139 if(horizontalKey>0 && beforeKey<0) 140 { 141 dashTime=0.0f; 142 } 143 else if(horizontalKey<0 && beforeKey>0) 144 { 145 dashTime=0.0f; 146 } 147 148 beforeKey=horizontalKey; 149 150 //アニメーションカーブを適用 151 xSpeed*=dashCurve.Evaluate(dashTime); 152 153 return xSpeed; 154 } 155 156 /// <summary> 157 /// アニメーションを設定する 158 /// </summary> 159 private void SetAnimation() 160 { 161 anim.SetBool("jumo",isjump); 162 anim.SetBool("ground",isGround); 163 anim.SetBool("run",isRun); 164 } 165}

投稿2023/01/25 00:29

Y0241-N

総合スコア1066

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

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

igu..

2023/01/25 00:57

わざわざありがとうございます🙇🏻‍♀️
guest

0

{ }の対応がメチャクチャです。

  • {は18回出てきますが、}は19回出てきます。
  • Update{ を閉じる前に private float GetYSpeed() の定義が出てきます。
  • まともなエディタを使えば{ }の対応を見て自動でインデントをつけてくれます。するとこの手の間違いは一発で見つかります。

投稿2023/01/25 00:22

ozwk

総合スコア13521

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

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

igu..

2023/01/25 01:06

すみません、、今後気をつけます!
guest

0

ベストアンサー

Updateの中にメソッド書いてるのでエラー出てます

投稿2023/01/24 22:47

UnchFullburst

総合スコア663

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

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

igu..

2023/01/25 00:18

ありがとうございます、無事解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問