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

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

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

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

Q&A

解決済

1回答

4558閲覧

【Unity・C#】Unityのタップゲーム作成で Argument 1: cannot convert from 'UnityEngine.GameObject' to 'int' のエラーが出る

muuu_u

総合スコア7

Unity

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

0グッド

0クリップ

投稿2020/02/16 06:17

編集2020/02/16 06:49

前提・実現したいこと
Unity初心者です。
本を参考にタップゲームを作成しようとしているのですが、
エラー文で詰まってしまっております。

発生している問題・エラーメッセージ
Unityのコンソールで以下のような文がエラーとして出ております。

Assets/Scripts/GameManager.cs(122,74): error CS1503: Argument 1: cannot convert from 'UnityEngine.GameObject' to 'int'

GameManagar.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class GameManager : MonoBehaviour { //定数定義 private const int MAX_ORB = 10; //オーブ最大数 private const int RESPAWN_TIME = 5; //オーブが発生する秒数 private const int MAX_LEVEL = 2; //最大レベル //オブジェクト参照 public GameObject orbPrefab; //オーブのプレハブ指定 public GameObject lvupPrefab; //レベルアップ時のエフェクト public GameObject canvasGame; //ゲームキャンバス public GameObject textScore; //スコアテキスト public GameObject lvupImage; //レベルアップイラスト public GameObject clearEffectPrefab; //ゲームをクリアした際のエフェクト //メンバ変数 private int score = 0; //現在のスコア private int nextScore = 10; //レベルアップまでに必要なスコア private int currentOrb = 0; //現在のオーブ数 private int levelRank = 0; //レベルランク private DateTime lastDateTime; //前回のオーブを生成した時間 private int [] nextScoreTable = new int[] {10, 10, 10} ; //レベルアップ数 // Start is called before the first frame update void Start() { //初期オーブ生成 currentOrb = 10; for (int i = 0; i < currentOrb; i++) { CreateOrb (); } //初期設定(スタートメソッド内で画像を初期状態へセットする。 lastDateTime = DateTime.UtcNow; nextScore = nextScoreTable [levelRank]; lvupImage.GetComponent<ObjectManager> ().SetLvupPicture(levelRank); RefreshScoreText (); } // Update is called once per frame void Update() { if (currentOrb < MAX_ORB) { TimeSpan timeSpan = DateTime.UtcNow - lastDateTime; if(timeSpan >= TimeSpan.FromSeconds (RESPAWN_TIME)) { while (timeSpan >= TimeSpan.FromSeconds (RESPAWN_TIME)) { createNewOrb (); timeSpan -= TimeSpan.FromSeconds (RESPAWN_TIME); } } } } //新しいオーブの生成 public void createNewOrb () { lastDateTime = DateTime.UtcNow; if (currentOrb >= MAX_ORB) { return; } CreateOrb (); currentOrb++; } //オーブ生成 public void CreateOrb () { GameObject orb = (GameObject)Instantiate (orbPrefab); orb.transform.SetParent (canvasGame.transform, false); orb.transform.localPosition = new Vector3 ( UnityEngine.Random.Range (-300.0f, 300.0f), UnityEngine.Random.Range (-140.0f, -500.0f), 0f); } //オーブ入手 public void GetOrb () { score += 1; // レベルアップ値を越えないよう制限 if (score > nextScore) { score = nextScore; } LevelUpEffect (); RefreshScoreText (); //ゲームクリア判定 if ((score == nextScore) && (levelRank == MAX_LEVEL)) { LevelMax (); } currentOrb--; } //スコアテキスト更新 void RefreshScoreText () { textScore.GetComponent<Text> ().text = "オーブ: " + score + " / " + nextScore; } //画像のレベル管理 void LevelUp () { if (score >= nextScore) { if (levelRank < MAX_LEVEL) { levelRank++; score = 0; LevelUpEffect (); nextScore = nextScoreTable [levelRank]; lvupImage.GetComponent<ObjectManager> ().SetLvupPicture (lvupImage); } } } //レベルアップ時の演出 void LevelUpEffect () { GameObject lvupEf = (GameObject)Instantiate (lvupPrefab); lvupEf.transform.SetParent(canvasGame.transform, false); lvupEf.transform.SetSiblingIndex (2); Destroy (lvupEf, 0.5f); } //レベルが限界値まで到達した時の演出 void LevelMax () { GameObject clearEf = (GameObject)Instantiate (clearEffectPrefab); clearEf.transform.SetParent (canvasGame.transform, false); } }

##ObjectManagar.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ObjectManager : MonoBehaviour { public Sprite[] rankupPicture = new Sprite[3];//レベルアップ用のイラスト表示 // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } //レベルアップ差分の絵を追加 public void SetLvupPicture (int level) { GetComponent<Image> ().sprite = rankupPicture [level]; } }

試したこと
int型でコンバートできないというエラーなので、
int型にあてはめられない物を何か引数で渡して代入しようとしているのかと思うのですが、
自分では解決ができませんでした。

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

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

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

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

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

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

fiveHundred

2020/02/16 06:44

ObjectManager(特にSetLvupPicture())のソースコードも記載してください。
muuu_u

2020/02/16 06:49

ありがとうございます、 ObjectManagar.csのコードを追記致しました。
guest

回答1

0

ベストアンサー

ObjectManagerを見た感じ、全く想定と違う使い方をしています。
SetLvupPicture()の引数には、画像のオブジェクトではなく、画像の番号を指定するようになっております。
その表示する画像はrankupPictureに事前に指定するようになっており、そのrankupPictureの番号が引数の番号に対応します。

投稿2020/02/16 07:18

fiveHundred

総合スコア9917

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

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

muuu_u

2020/02/16 08:36

ありがとうございます、どうやら画像そのものを渡そうとしていたようでして ランクの値を渡すようにした所無事にエラーを解決する事ができました。 ``` nextScore = nextScoreTable [levelRank]; lvupImage.GetComponent<ObjectManager> ().SetLvupPicture (levelRank); ```
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問