解決したい事
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;
}
}
}