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

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

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

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

Unity

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

Q&A

解決済

2回答

1263閲覧

【Unity・C#】Unityのタップゲームの作成において変数周りでエラーが表示される

muuu_u

総合スコア7

C#

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

Unity

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

0グッド

0クリップ

投稿2020/02/16 03:02

前提・実現したいこと

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

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

Unityのコンソールで以下のような文がエラーとして出ております。

Assets/Scripts/GameManager.cs(99,13): error CS1955: Non-invocable member 'GameManager.clearEffect' cannot be used like a method. Assets/Scripts/GameManager.cs(129,54): error CS0103: The name 'lvup_efPrefab' does not exist in the current context Assets/Scripts/GameManager.cs(139,55): error CS0103: The name 'lvup_efPrefab' does not exist in the current context

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5using UnityEngine.UI; 6using System; 7 8public class GameManager : MonoBehaviour { 9//定数定義 10private const int MAX_ORB = 10; //オーブ最大数 11private const int RESPAWN_TIME = 5; //オーブが発生する秒数 12private const int MAX_LEVEL = 2; //最大レベル 13 14//オブジェクト参照 15public GameObject orbPrefab; //オーブのプレハブ指定 16public GameObject lvupEf; //レベルアップ時のエフェクト 17public GameObject canvasGame; //ゲームキャンバス 18public GameObject textScore; //スコアテキスト 19public GameObject lvupImage; //レベルアップイラスト 20public GameObject clearEffect; //ゲームをクリアした際のエフェクト 21 22//メンバ変数 23private int score = 0; //現在のスコア 24private int nextScore = 10; //レベルアップまでに必要なスコア 25 26private int currentOrb = 0; //現在のオーブ数 27 28private int levelRank = 0; //レベルランク 29 30private DateTime lastDateTime; //前回のオーブを生成した時間 31 32private int [] nextScoreTable = new int[] {10, 10, 10} ; //レベルアップ数 33 34 // Start is called before the first frame update 35 void Start() { 36 //初期オーブ生成 37 currentOrb = 10; 38 for (int i = 0; i < currentOrb; i++) { 39 CreateOrb (); 40 } 41 42 //初期設定(スタートメソッド内で画像を初期状態へセットする。 43 lastDateTime = DateTime.UtcNow; 44 nextScore = nextScoreTable [levelRank]; 45 lvupImage.GetComponent<ObjectManager> ().SetLvupPicture(levelRank); 46 47 RefreshScoreText (); 48 } 49 50 // Update is called once per frame 51 void Update() { 52 if (currentOrb < MAX_ORB) { 53 TimeSpan timeSpan = DateTime.UtcNow - lastDateTime; 54 55 if(timeSpan >= TimeSpan.FromSeconds (RESPAWN_TIME)) { 56 while (timeSpan >= TimeSpan.FromSeconds (RESPAWN_TIME)) { 57 createNewOrb (); 58 timeSpan -= TimeSpan.FromSeconds (RESPAWN_TIME); 59 } 60 } 61 62 } 63 } 64 65 66 //新しいオーブの生成 67 public void createNewOrb () { 68 lastDateTime = DateTime.UtcNow; 69 if (currentOrb >= MAX_ORB) { 70 return; 71 } 72 CreateOrb (); 73 currentOrb++; 74 } 75 76 //オーブ生成 77 public void CreateOrb () { 78 GameObject orb = (GameObject)Instantiate (orbPrefab); 79 orb.transform.SetParent (canvasGame.transform, false); 80 orb.transform.localPosition = new Vector3 ( 81 UnityEngine.Random.Range (-300.0f, 300.0f), 82 UnityEngine.Random.Range (-140.0f, -500.0f), 83 0f); 84 } 85 86 //オーブ入手 87 public void GetOrb () { 88 score += 1; 89 90 if (score > nextScore) { 91 score = nextScore; 92 } 93 94 LevelUp (); 95 RefreshScoreText (); 96 97 //ゲームクリア判定 98 if ((score == nextScore) && (levelRank == MAX_LEVEL)) { 99 clearEffect (); 100 } 101 102 currentOrb--; 103 } 104 105 //スコアテキスト更新 106 void RefreshScoreText () { 107 textScore.GetComponent<Text> ().text = 108 "オーブ: " + score + " / " + nextScore; 109 } 110 111 //画像のレベル管理 112 void LevelUp () { 113 if (score >= nextScore) { 114 if (levelRank < MAX_LEVEL) { 115 levelRank++; 116 score = 0; 117 118 LevelUp (); 119 120 nextScore = nextScoreTable [levelRank]; 121 lvupImage.GetComponent<ObjectManager> ().SetLvupPicture (levelRank); 122 123 } 124 } 125 } 126 127 //レベルアップ時の演出 128 void LevelUpEffect () { 129 GameObject lvupEf = (GameObject)Instantiate (lvup_efPrefab); 130 131 lvupEf.transform.SetParent(canvasGame.transform, false); 132 lvupEf.transform.SetSiblingIndex (2); 133 134 Destroy (lvupEf, 0.5f); 135 } 136 137 //レベルが限界値まで到達した時の演出 138 void LevelMax () { 139 GameObject clearEf = (GameObject)Instantiate (lvup_efPrefab); 140 clearEf.transform.SetParent (canvasGame.transform, false); 141 } 142}

試したこと

エラー文章で検索をかけましたが、思うような回答が得られませんでした。
メソッドとして使えない、呼び出し不可能なメンバー関数となっております。
何か変数の指定方法が間違っているのかもしれませんが、自分では解決ができませんでした。

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

Unity2019.2.5.f1

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

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

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

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

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

guest

回答2

0

ベストアンサー

C#

1public GameObject clearEffect; //ゲームをクリアした際のエフェクト

C#

1clearEffect ();

変数をメソッドとして呼んでいますが、これは何でしょう?

C#

1GameObject lvupEf = (GameObject)Instantiate (lvup_efPrefab);

C#

1GameObject clearEf = (GameObject)Instantiate (lvup_efPrefab);

唐突にlvup_efPrefabが出てきましたが、これは何者ですか?

投稿2020/02/16 04:05

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

muuu_u

2020/02/16 04:20 編集

C#も理解が浅いためトンチンカンなコードを記載していたらすみません。 ・変数として定義していたものをメソッドとして呼び出そうとしていたので、  エラーが起こっておりました。 > if ((score == nextScore) && (levelRank == MAX_LEVEL)) { > clearEffect (); > } こちらの条件文で呼び出そうとしているのはメソッドなのですね。 該当箇所を修正するとエラーが解決されました。 ・lvup_efPrefabですが、アセットにあるprefabのデータを読み込んでいるのかと思ってたのですが、  そもそもオブジェクトの参照を定義していなかったのでエラーを引き起こしておりました。  こちらオブジェクトの参照に記載を行うとエラーを解決する事ができました。 ご指摘ありがとうございました、 再考をして記載箇所を修正するとエラーが解決されました。
guest

0

コードを修正する事で該当エラーを修正する事ができましたが、
こちらのエラーが残った状態となりました。

Assets/Scripts/GameManager.cs(140,42): error CS1501: No overload for method 'Instantiate' takes 0 arguments

インスタンスに対する多重定義がされていないようなエラーが出てしまっております、
宜しければ引き続き回答よろしくお願い致します。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5using UnityEngine.UI; 6using System; 7 8public class GameManager : MonoBehaviour { 9//定数定義 10private const int MAX_ORB = 10; //オーブ最大数 11private const int RESPAWN_TIME = 5; //オーブが発生する秒数 12private const int MAX_LEVEL = 2; //最大レベル 13 14//オブジェクト参照 15public GameObject orbPrefab; //オーブのプレハブ指定 16public GameObject lvupPrefab; //レベルアップ時のエフェクト 17public GameObject canvasGame; //ゲームキャンバス 18public GameObject textScore; //スコアテキスト 19public GameObject lvupImage; //レベルアップイラスト 20public GameObject clearEffectPrefab; //ゲームをクリアした際のエフェクト 21 22//メンバ変数 23private int score = 0; //現在のスコア 24private int nextScore = 10; //レベルアップまでに必要なスコア 25 26private int currentOrb = 0; //現在のオーブ数 27 28private int levelRank = 0; //レベルランク 29 30private DateTime lastDateTime; //前回のオーブを生成した時間 31 32private int [] nextScoreTable = new int[] {10, 10, 10} ; //レベルアップ数 33 34 // Start is called before the first frame update 35 void Start() { 36 //初期オーブ生成 37 currentOrb = 10; 38 for (int i = 0; i < currentOrb; i++) { 39 CreateOrb (); 40 } 41 42 //初期設定(スタートメソッド内で画像を初期状態へセットする。 43 lastDateTime = DateTime.UtcNow; 44 nextScore = nextScoreTable [levelRank]; 45 lvupImage.GetComponent<ObjectManager> ().SetLvupPicture(levelRank); 46 47 RefreshScoreText (); 48 } 49 50 // Update is called once per frame 51 void Update() { 52 if (currentOrb < MAX_ORB) { 53 TimeSpan timeSpan = DateTime.UtcNow - lastDateTime; 54 55 if(timeSpan >= TimeSpan.FromSeconds (RESPAWN_TIME)) { 56 while (timeSpan >= TimeSpan.FromSeconds (RESPAWN_TIME)) { 57 createNewOrb (); 58 timeSpan -= TimeSpan.FromSeconds (RESPAWN_TIME); 59 } 60 } 61 62 } 63 } 64 65 66 //新しいオーブの生成 67 public void createNewOrb () { 68 lastDateTime = DateTime.UtcNow; 69 if (currentOrb >= MAX_ORB) { 70 return; 71 } 72 CreateOrb (); 73 currentOrb++; 74 } 75 76 //オーブ生成 77 public void CreateOrb () { 78 GameObject orb = (GameObject)Instantiate (orbPrefab); 79 orb.transform.SetParent (canvasGame.transform, false); 80 orb.transform.localPosition = new Vector3 ( 81 UnityEngine.Random.Range (-300.0f, 300.0f), 82 UnityEngine.Random.Range (-140.0f, -500.0f), 83 0f); 84 } 85 86 //オーブ入手 87 public void GetOrb () { 88 score += 1; 89 90 // レベルアップ値を越えないよう制限 91 if (score > nextScore) { 92 score = nextScore; 93 } 94 95 LevelUpEffect (); 96 RefreshScoreText (); 97 98 //ゲームクリア判定 99 if ((score == nextScore) && (levelRank == MAX_LEVEL)) { 100 LevelMax (); 101 } 102 103 currentOrb--; 104 } 105 106 //スコアテキスト更新 107 void RefreshScoreText () { 108 textScore.GetComponent<Text> ().text = 109 "オーブ: " + score + " / " + nextScore; 110 } 111 112 //画像のレベル管理 113 void LevelUp () { 114 if (score >= nextScore) { 115 if (levelRank < MAX_LEVEL) { 116 levelRank++; 117 score = 0; 118 119 LevelUpEffect (); 120 121 nextScore = nextScoreTable [levelRank]; 122 lvupImage.GetComponent<ObjectManager> ().SetLvupPicture (levelRank); 123 124 } 125 } 126 } 127 128 //レベルアップ時の演出 129 void LevelUpEffect () { 130 GameObject lvupEf = (GameObject)Instantiate (lvupPrefab); 131 132 lvupEf.transform.SetParent(canvasGame.transform, false); 133 lvupEf.transform.SetSiblingIndex (2); 134 135 Destroy (lvupEf, 0.5f); 136 } 137 138 //レベルが限界値まで到達した時の演出 139 void LevelMax () { 140 GameObject clearEf = (GameObject)Instantiate (); 141 clearEf.transform.SetParent (canvasGame.transform, false); 142 } 143} 144

投稿2020/02/16 04:35

muuu_u

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問