質問するログイン新規登録
C#

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

Q&A

1回答

1103閲覧

error CS0029: Cannot implicitly convert type 'int[]' to 'int'を解決したい。

_nagurikomi

総合スコア1

C#

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

0グッド

0クリップ

投稿2023/10/06 13:24

0

0

実現したいこと

rror CS0029: Cannot implicitly convert type 'int[]' to 'int'を解決したい。

前提

ここに質問の内容を詳しく書いてください。
(例)
TypeScriptで●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。

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

Assets\Scripts\Player.cs(137,23): error CS0029: Cannot implicitly convert type 'int[]' to 'int'

該当のソースコード

c#

1 2using System; 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine.UI; 6using UnityEngine; 7 8public class Player : MonoBehaviour 9{ 10 11 //カメラ 12 Camera cam; 13 14 //カメラ揺れ 15 public CameraShake shake; 16 17 //ステータス 18 public int hp = 100; 19 int maxHp = 100; 20 21 [HideInInspector] public int OverHeat = 0; 22 23 //あたり判定 24 public BoxCollider2D col; 25 26 //シールド 27 public GameObject shield; //反射 28 public Transform ShieldF; 29 30 [HideInInspector] public float leftShield; 31 public float shieldCt; 32 bool IsShield = false; 33 34 public cTcShield cTcShield; 35 //hp制御 36 Slider slider; 37 Slider Bslider; 38 39 //被ダメージ 40 public int damage; 41 public GameObject hit; 42 43 44 //efect 45 public GameObject explosion; 46 47 48 public float speedx = 1f; 49 public float speedy = 1f; 50 51 public float xLimit = 100f; 52 public float yLimit = 4.5f; 53 54 //オーディオ 55 public AudioClip Shoot1; 56 public AudioClip powerUpSe; 57 AudioSource audioSource; 58 //ターン 59 //private bool turn = false; 60 61 //lv1通常機銃 62 public GameObject shoot1Prefab; 63 public Transform shotpoint1; 64 public GameObject shoot2Prefab; 65 public Transform shotpoint2; 66 public GameObject shoot3Prefab; 67 public Transform shotpoint3; 68 public GameObject shoot4Prefab; 69 public Transform shotpoint4; 70 71 //スキル 72 public GameObject Item_slot1; 73 public GameObject Item_slot2; 74 75 76 77 78 //ジェット 79 public GameObject jet; 80 81 //FireEfect 82 public GameObject Fefect; 83 public float FEcoolTime = 0.02f; 84 float FleftCoolTime; 85 86 87 //lv1スペシャルウェポン 88 public GameObject SPshootPrefab; 89 public Transform shotpointSP; 90 91 //ウルと 92 public GameObject UltPrefabs; 93 public Transform UltPoint; 94 95 public GameObject ultSlider; 96 97 //ss発射制御 98 public float coolTime = 1f; 99 [HideInInspector] public float leftCoolTime; 100 bool IsSS; 101 102 //発射制御 103 public float interval = 0.2f; // 何秒間隔で撃つか 104 private float timer = 0.0f; // 時間カウント用のタイマー 105 106 private const int _threshold = 30;//キーを押しているフレーム数を記録 107 private int _duration = 0; 108 109 public float span = 3f; 110 private float currentTime = 0f; 111 112 //インスタンス 113 private Animator anim = null; 114 private Rigidbody2D rb = null; 115 public cTcontroller ctc; 116 117 118 119 private int p_Item_num1; 120 private int p_Item_num2; 121 122 // Start is called before the first frame update 123 void Start() 124 { 125 rb = GetComponent<Rigidbody2D>(); 126 cam = Camera.main; 127 slider = GameObject.Find("Slider").GetComponent<Slider>(); 128 slider.value = 1; 129 Bslider = GameObject.Find("BSlider").GetComponent<Slider>(); 130 Bslider.value = 0; 131 anim = GetComponent<Animator>(); 132 // leftCoolTime = 15f; 133 col.enabled = true; 134 audioSource = GetComponent<AudioSource>(); 135 jet.SetActive(false); 136 GameManager.Instance.BossDie = false; 137 p_Item_num1 = GameManager.Instance.Item_num; 138 Item_slot1 = GameManager.Instance.Item_data[p_Item_num1]; 139 140 } 141 142 // Update is called once per frame 143 void FixedUpdate() 144 { 145 146 //UnityEngine.Debug.Log(speedx); 147 148 float x = Input.GetAxisRaw("Horizontal"); //キー判定 149 float y = Input.GetAxisRaw("Vertical"); //キー判定 150 151 152 //移動 153 if (Input.GetAxisRaw("Horizontal") != 0) 154 { 155 transform.position += new Vector3(speedx * x, 0, 0); 156 157 } 158 159 if (Input.GetAxisRaw("Vertical") != 0) 160 { 161 transform.position += new Vector3(0, speedy * y, 0); 162 } 163 164 165 166 Vector3 currentPos = transform.position; 167 // オブジェクトが移動できる範囲の制限 168 currentPos.x = Mathf.Clamp(currentPos.x, -xLimit, +xLimit); 169 currentPos.y = Mathf.Clamp(currentPos.y, -yLimit, yLimit); 170 171 transform.position = currentPos; 172 173 if (x != 0 || y != 0) 174 { 175 jet.SetActive(true); 176 } 177 else 178 { 179 jet.SetActive(false); 180 } 181 182 //ss発射 183 IsSS = ctc.isSS; 184 leftCoolTime -= Time.deltaTime; 185 if (leftCoolTime <= 0 ) 186 { 187 if (Input.GetKey(KeyCode.Z) ) 188 { 189 // SShot1(); 190 Instantiate(SPshootPrefab, shotpointSP.position, shotpointSP.rotation); 191 leftCoolTime = coolTime; 192 } 193 194 } 195 196 //シールド 197 IsShield = cTcShield.isShield; 198 leftShield -= Time.deltaTime; 199 if (leftShield <= 0) 200 { 201 if (Input.GetKey(KeyCode.Q)) 202 { 203 // SShot1(); 204 reflectShield(); 205 } 206 207 } 208 209 //ウルと 210 if (Input.GetKey(KeyCode.C)) 211 { 212 Ult(); 213 } 214 215 //バフ 216 FleftCoolTime -= Time.deltaTime; 217 if (FleftCoolTime <= 0) 218 { 219 if (Input.GetKey(KeyCode.E)) 220 { 221 222 Fireefect(); 223 } 224 } 225 226 //発射 227 if (Input.GetKey(KeyCode.Space) && timer <= 0.0f && OverHeat <= 100) 228 { 229 230 Instantiate(shoot1Prefab, shotpoint1.position, shotpoint1.rotation); 231 //Instantiate(shoot2Prefab, shotpoint2.position, shotpoint2.rotation); 232 //Instantiate(shoot3Prefab, shotpoint3.position, shotpoint3.rotation); 233 Instantiate(shoot4Prefab, shotpoint4.position, shotpoint4.rotation); 234 235 236 audioSource.PlayOneShot(Shoot1); 237 238 OverHeat += 1; 239 Bslider.value += 0.01f; 240 241 _duration = _duration + 1; 242 timer = interval; 243 } 244 if (Input.GetKeyUp(KeyCode.Space)) 245 { 246 _duration = 0;//押しているフレーム数をリセット 247 } 248 249 //長押しを判定 250 if (_duration == _threshold) 251 { 252 UnityEngine.Debug.Log("長押し"); 253 } 254 if (timer > 0.0f) 255 { 256 timer -= Time.deltaTime; 257 } 258 259 currentTime += Time.deltaTime; 260 261 if (currentTime > span && !Input.GetKey(KeyCode.Space)) 262 { 263 264 currentTime = 0f; 265 if (OverHeat > 0) 266 { 267 Bslider.value -= 0.01f; 268 OverHeat -= 1; 269 } 270 271 } 272 273 } 274 275 public void OnDamage() 276 { 277 UnityEngine.Debug.Log("被弾"); 278 Instantiate(hit, transform.position, transform.rotation); 279 hp -= damage; 280 shake.Shake(0.25f, 0.1f); 281 slider.value -= damage * 0.01f; 282 if (hp <= 0) 283 { 284 Instantiate(explosion, transform.position, transform.rotation); 285 Destroy(gameObject); 286 287 } 288 } 289 290 public void SShot1() 291 { 292 if (IsSS) 293 { 294 Instantiate(SPshootPrefab, shotpointSP.position, shotpointSP.rotation); 295 leftCoolTime = coolTime; 296 } 297 298 } 299 300 private void Reset() 301 { 302 speedx = 0.1f; 303 speedy = 0.1f; 304 col.enabled = true; 305 } 306 307 public void reflectShield() 308 { 309 310 if (IsShield) 311 { 312 Instantiate(shield, ShieldF.position, ShieldF.rotation); 313 leftShield = shieldCt; 314 } 315 316 } 317 318 public void Fireefect() 319 { 320 GameObject childObject = Instantiate(Fefect, transform); 321 childObject.transform.localPosition = new Vector3(0, 0, 0); 322 childObject.transform.localRotation = Quaternion.identity; 323 childObject.transform.localScale = new Vector3(10, 10, 1); 324 325 FleftCoolTime = FEcoolTime; 326 speedx += 0.05f; 327 speedy += 0.05f; 328 329 audioSource.PlayOneShot(powerUpSe); 330 331 if (hp <= 80) 332 { 333 hp += 20; 334 slider.value += 0.2f; 335 } 336 else 337 { 338 hp = 100; 339 slider.value = 1f; 340 } 341 342 343 Invoke("Reset",7f); 344 } 345 346 public void Ult() 347 { 348 Instantiate(UltPrefabs, UltPoint.position, UltPoint.rotation); 349 } 350 351 public void Item1() 352 { 353 354 } 355 public void Item2() 356 { 357 358 } 359} 360 361 362 363 364

なぜint型にint型を代入しようとするとエラーが発生してしまうのでしょうか。

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

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

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

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

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

YAmaGNZ

2023/10/06 22:08

エラーメッセージを見る限り配列を入れようとしているんじゃないんですか?
guest

回答1

0

Assets\Scripts\Player.cs(137,23): error CS0029: Cannot implicitly convert type 'int[]' to 'int'

Player.csの137行目23文字の場所でint[]intにキャスト(変換)できませんというエラーです。

137行目は

csharp

1 p_Item_num1 = GameManager.Instance.Item_num;

ですが、23文字目以降のGameManager.Instance.Item_numint[]のせいと推測されます。

原因となっているGameManagerはあなたの書かれた別のコードですので
そちらを直すべきなのかこのコードを直すべきかはこれだけでは判断できません。

投稿2023/10/07 02:08

編集2023/10/07 16:06
hqf00342

総合スコア400

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.30%

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

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

質問する

関連した質問