Unity 迷路ゲーム
非常に長いのですが、一向に解決しないので質問させてください。
迷路ゲームを参考書をもとにscriptを描いたのですが、
UnityException: Tag: sphere is not defined!
MazeAppScript.ReSet () (at Assets/MazeAppScript.cs:73)
MazeAppScript.Start () (at Assets/MazeAppScript.cs:19)
のエラーが出てしまいました。
CreateSphere()
メソッドでタグは追加してるのですが、どうにもうまくいきません、恐縮ですがご指摘どうかお願いいたします。
using UnityEngine;
using System.Collections;
public class MazeAppScript : MonoBehaviour {
private System.Random rnd;
private bool endFlg = false;
public int power = 100;
public int gameTime = 300;
private int playTime = 300;
private int endTime = 300;
public int mazeSize = 10;
public float mazeLevel = 1;
private int hiScore = 0;
private bool toolFlg = false;
// Use this for initialization
void Start () {
rnd = new System.Random (System.Environment.TickCount);
LoadPref ();
ReSet ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
toolFlg = !toolFlg;
if(!toolFlg){SavePref();}
}
if (endFlg) { return; }
playTime = endTime - (int)Time.time;
CheckTime ();
}
void OnGuI(){
GUI.Box (new Rect (10, 10, 100, 100), "POWRE/TIME");
GUI.Label (new Rect (20, 40, 80, 20), "power:" + power);
GUI.Label (new Rect (20, 70, 80, 20), "TIME:" + playTime);
if (toolFlg) {
GUI.Box (new Rect(120,10,100,40),"Size");
mazeSize = (int)(GUI.HorizontalSlider(new Rect(130,30,80,20),
mazeSize,5f,30f));
GUI.Box(new Rect(120,60,100,40),"Level");
mazeLevel = GUI.HorizontalSlider(new Rect(130,30,80,20)
,mazeLevel,0,10f);
GUI.Box(new Rect(120,110,100,50),"Restart");
if(GUI.Button (new Rect(130,130,80,20),"DO-IT!")){
ReSet();
}
}
}
void LoadPref(){
int loadSize = PlayerPrefs.GetInt ("mazeSize");
mazeLevel = PlayerPrefs.GetFloat ("mazeLevel");
hiScore = PlayerPrefs.GetInt ("hiScore");
if (loadSize != 0) {
mazeSize = loadSize;
}
}
void SavePref(){
PlayerPrefs.SetInt ("mazeSize", mazeSize);
PlayerPrefs.SetFloat ("mazeLevel", mazeLevel);
}
void ReSet(){
SavePref ();
GameObject[] walls = GameObject.FindGameObjectsWithTag("ob_wall");
foreach (GameObject obj in walls) {
GameObject.Destroy(obj);
}
GameObject[] sps = GameObject.FindGameObjectsWithTag("sphere");
foreach (GameObject obj in sps) {
GameObject.Destroy(obj);
}
GameObject.Find ("GUI Text").guiText.text = "";
CreateMazeData ();
CreateSphere ();
power = 100;
endTime = gameTime + (int)Time.time;
toolFlg = false;
endFlg = false;
}
void CreateMazeData(){
int mazeW = mazeSize * 4 + 2;
bool[,] fdata = new bool[mazeW, mazeW];
for (int i = 0; i < mazeW; i++) {
for (int j = 0; j < mazeW; i++) {
if(i == 0 || i == (mazeW - 1)||
j == 0 || j == (mazeW - 1)){
fdata[i,j] = true;
}else{
fdata[i,j] = false;
}
}
}
int[,] arw = new int[,]{
{0,-1},{0,1},{-1,0},{1,0}
};
for (int i = 0; i < (mazeSize / 2)*(mazeSize / 2); i++) {
while(true){
int x = rnd.Next (1, mazeSize) * 4;
int y = rnd.Next (1, mazeSize) * 4;
int n = i % 4;
fdata[x,y] = true;
while(true){
x += arw[n,0];
y += arw[n,1];
if(fdata[x,y]){
break;
}else{
fdata[x,y] = true;
}
}
break;
}
}
int cp = mazeW / 2;
fdata[cp,cp] = false;
GameObject.Find("DefaultAvatar").transform.position = new Vector3(cp, 0, cp);
CreateMaze (fdata);
int [,] gdatas = new int[,]{
{1,1},{1,mazeW},{mazeW - 2,1},{mazeW - 2 ,mazeW - 2}
};
int gn = rnd.Next (4);
Vector3 goalpos = new Vector3 (gdatas [gn, 0], 1.5f, gdatas [gn, 1]);
GameObject.Find ("goal").transform.position = goalpos;
}
void CreateMaze(bool[,] data){
int mazeW = mazeSize * 4 + 2;
Texture txtr = (Texture)Resources.Load("Grass&Rock");
for (int i = 0; i < mazeW; i++) {
for(int j = 0; j < mazeW ; j++){
if(data[i,j]){
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.tag = "ob_wall";
obj.transform.localScale = new Vector3(1,2,1);
obj.transform.position = new Vector3(i,1,j);
obj.renderer.material.mainTexture = txtr;
obj.collider.isTrigger = true;
GameObject obj2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj2.tag = "ob_wall";
obj2.transform.localScale = new Vector3(0.9f,0.9f,0.9f);
obj2.transform.position = new Vector3(i,1,j);
}
}
}
}
void CreateSphere(){
for (int i = 0; i < (mazeSize / 2); i++) {
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.tag = "sphere";
obj.renderer.material.color = new Color(1,0,0,0.5f);
obj.renderer.material.shader = Shader.Find("Transparent/Diffuse");
obj.AddComponent("Rigidbody");
obj.transform.position = new Vector3(rnd.Next(mazeSize) * 4 + 2, 0, rnd.Next(mazeSize) * 4 + 1);
obj.AddComponent("MazeSphereScript");
}
}
public bool IsEnd(){
return endFlg;
}
public void LossPower(int n){
power -= n;
if(power <= 0){
power = 0;
BadEnd();
}
}
public void CheckTime(){
if (playTime <= 0) {
BadEnd();
}
}
public void BadEnd(){
endFlg = true;
int score = (int)(power * mazeLevel + playTime * mazeSize);
GameObject obj = GameObject.Find ("GUI TEXT");
obj.guiText.color = Color.blue;
obj.guiText.text = "GAME OVER" + "\n" + "SCORE:" + score;
PlayerPrefs.SetInt ("hiscore", 1);
}
public void GoodEnd(){
endFlg = true;
GameObject obj = GameObject.Find("GUI Text");
int score = (int)(power * mazeLevel * 2 + playTime * mazeSize * 2);
string msg = "CLEAR!!";
obj.guiText.color = Color.yellow;
if (score > hiScore) {
hiScore = score;
msg = "Hi-score";
PlayerPrefs.SetInt ("hiScore", hiScore);
obj.guiText.color = Color.red;
}
msg += "\n" + "SCORE:" + score;
obj.guiText.text = msg;
}