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

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

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

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

Unity

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

Q&A

解決済

1回答

797閲覧

【Unity・C#】Unityのタップゲーム作成でレベルの処理がうまく走らない

muuu_u

総合スコア7

C#

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

Unity

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

0グッド

0クリップ

投稿2020/02/22 15:03

##解決したい事
Unityでタップゲームを作成しています。

・スコアが貯まると、レベルアップが走って次のレベルの絵に差し替えるような仕組みを組み上げているのですが
レベルの到達値に達してもうまく処理が走らず(初期MAX10に到達しても次の100値に行きません)
・オーブを3種類設定して配置をしているのですが、何故か一種類しか生成されません。

#エラー文

UnassignedReferenceException: The variable lvupImage of GameManager has not been assigned. You probably need to assign the lvupImage variable of the GameManager script in the inspector. UnityEngine.GameObject.GetComponent[T] () (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28) GameManager.Start () (at Assets/Scripts/GameManager.cs:72)

UnassignedReferenceException:GameManagerの変数lvupImageが割り当てられておらず
インスペクターでGameManagerスクリプトのlvupImage変数を割り当てる必要があるようなのですが
こちらはどういうことになるのでしょうか?
ご存知でしたら回答お願い致します。

###GameManager.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; //最大レベル // データセーブ用キー private const string KEY_SCORE = "SCORE"; // スコア private const string KEY_LEVEL = "LEVEL"; // レベル private const string KEY_ORB = "ORB"; // オーブ数 private const string KEY_TIME = "TIME"; // 時間 //オブジェクト参照 public GameObject orbPrefab; //オーブのプレハブ指定 public GameObject lvupPrefab; //レベルアップ時のエフェクト public GameObject canvasGame; //ゲームキャンバス public GameObject textScore; //スコアテキスト public GameObject lvupImage; //レベルアップイラスト public GameObject clearEffectPrefab; //ゲームをクリアした際のエフェクト public AudioClip getScoreSE; // SEタップ時 public AudioClip levelUpSE; // レベルアップ時 public AudioClip clearSE; // 効果音クリア //メンバ変数 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, 100, 1000} ; //レベルアップ数 private AudioSource audioSource; //オーディオソース // Start is called before the first frame update void Start() { //オーディオソース取得 audioSource = this.gameObject.GetComponent<AudioSource> (); // 初期設定 score = PlayerPrefs.GetInt (KEY_SCORE, 0); levelRank = PlayerPrefs.GetInt (KEY_LEVEL, 0); currentOrb = PlayerPrefs.GetInt (KEY_ORB, 10); // 初期オーブ生成 for (int i = 0; i < MAX_ORB; i++) { CreateOrb (); } // 時間の復元 string time = PlayerPrefs.GetString (KEY_TIME, ""); if (time == "") { // 時間がセーブされていない場合は現在時刻を使用 lastDateTime = DateTime.UtcNow; } else { long temp = Convert.ToInt64 (time); lastDateTime = DateTime.FromBinary (temp); } 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); //オーブの種類を設定 int kind = UnityEngine.Random.Range(0, levelRank + 1); switch (kind) { case 0: orb.GetComponent<OrbManager> ().SetKind (OrbManager.ORB_KIND.BLUE); break; case 1: orb.GetComponent<OrbManager> ().SetKind (OrbManager.ORB_KIND.GREEN); break; case 2: orb.GetComponent<OrbManager> ().SetKind (OrbManager.ORB_KIND.PURPLE); break; } } //オーブ入手 public void GetOrb (int getScore) { audioSource.PlayOneShot (getScoreSE); if (score < nextScore) { score += getScore; // レベルアップ値を越えないよう制限 if (score > nextScore) { score = nextScore; } LevelUpEffect (); RefreshScoreText (); //ゲームクリア判定 if ((score == nextScore) && (levelRank == MAX_LEVEL)) { LevelMax (); } } currentOrb--; } //スコアテキスト更新 void RefreshScoreText () { textScore.GetComponent<Text> ().text = "Orb: " + score + " / " + nextScore; } //画像のレベル管理 void LevelUp () { if (score >= nextScore) { if (levelRank < MAX_LEVEL) { levelRank++; score = 0; LevelUpEffect (); nextScore = nextScoreTable [levelRank]; lvupImage.GetComponent<ObjectManager> ().SetLvupPicture (levelRank); } } } //レベルアップ時の演出 void LevelUpEffect () { GameObject lvupEf = (GameObject)Instantiate (lvupPrefab); lvupEf.transform.SetParent(canvasGame.transform, false); lvupEf.transform.SetSiblingIndex (2); audioSource.PlayOneShot (levelUpSE); Destroy (lvupEf, 0.5f); } //レベルが限界値まで到達した時の演出 void LevelMax () { GameObject clearEf = (GameObject)Instantiate (clearEffectPrefab); clearEf.transform.SetParent (canvasGame.transform, false); audioSource.PlayOneShot (clearSE); } }

###ObjectManager.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]; } }

###OrbManager.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; public class OrbManager : MonoBehaviour { //オブジェクト参照 private GameObject gameManager; //ゲームマネージャー public Sprite[] orbPicture = new Sprite [3]; //オーブの絵 public enum ORB_KIND{ //オーブの種類を定義 BLUE, GREEN, PURPLE, } private ORB_KIND orbKind; //オーブの種類 // Start is called before the first frame update void Start() { gameManager = GameObject.Find("GameManager"); } // Update is called once per frame void Update() { } //オーブ取得 public void TouchOrb () { if (Input.GetMouseButton (0) == false) { return; } RectTransform rect = GetComponent<RectTransform> (); // オーブの軌跡設定 Vector3[] path = { new Vector3(rect.localPosition.x * 1.5f, 300f, 0f), //中間点 new Vector3(0f, 150f, 0f), //終点 }; // DOTweenを使ったアニメ作成 rect.DOLocalPath (path, 0.5f, PathType.CatmullRom) .SetEase (Ease.OutQuad) .OnComplete (AddOrbPoint); // 同時にサイズも変更 rect.DOScale ( new Vector3 (0.5f, 0.5f, 0f), 0.5f ); } // オーブアニメ終了後にポイント加算処理をする void AddOrbPoint () { switch (orbKind) { case ORB_KIND.BLUE: gameManager.GetComponent<GameManager> ().GetOrb (1); break; case ORB_KIND.GREEN: gameManager.GetComponent<GameManager> ().GetOrb (5); break; case ORB_KIND.PURPLE: gameManager.GetComponent<GameManager> ().GetOrb (10); break; } Destroy (this.gameObject); } //オーブの種類を設定 public void SetKind (ORB_KIND kind) { orbKind = kind; switch (orbKind) { case ORB_KIND.BLUE: GetComponent<Image> ().sprite = orbPicture [0]; break; case ORB_KIND.GREEN: GetComponent<Image> ().sprite = orbPicture [1]; break; case ORB_KIND.PURPLE: GetComponent<Image> ().sprite = orbPicture [2]; break; } } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

・スコアが貯まると、レベルアップが走って次のレベルの絵に差し替えるような仕組みを組み上げているのですが
レベルの到達値に達してもうまく処理が走らず(初期MAX10に到達しても次の100値に行きません)

LevelUp メソッドがどこからも呼び出されていないようですので、確認してみてください。

・オーブを3種類設定して配置をしているのですが、何故か一種類しか生成されません。

以下のコード行で、「levelRank」の値をデバッグしてください。

c#

1int kind = UnityEngine.Random.Range(0, levelRank + 1);

UnassignedReferenceException:GameManagerの変数lvupImageが割り当てられておらず
インスペクターでGameManagerスクリプトのlvupImage変数を割り当てる必要があるようなのですが
こちらはどういうことになるのでしょうか?

このメッセージの通りです。

「You probably need to assign the lvupImage variable of the GameManager script in the inspector.」

また、同様の質問がありましたので、こちらも参考にしてみてください。

Unity - 変数が代入されていないエラー|teratail
https://teratail.com/questions/51074

投稿2020/02/22 23:58

nskydiving

総合スコア6500

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

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

muuu_u

2020/02/23 06:22 編集

丁寧なご回答ありがとうございます、ご指摘通り【LevelUp】のメソッドを呼ぶべき箇所にエフェクトを呼ぶ メソッドを指定していたのが原因だったようです。 ``` if (score > nextScore) { score = nextScore; } LevelUp (); RefreshScoreText (); ``` 上記コードに修正しました。 またレベルアップ処理がうまく走ったらオーブが3種類生成されるようになりました。 解決することができ大変助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問