我的游戏中有一个暂停屏幕,有两个按钮:resume game
和exit to main menu
主菜单只有1个按钮,希望是play
,
当我点击exit to main menu
加载主菜单级别时,如果我点击Play
我的游戏级别场景没有重新开始,而是继续所有内容,包括所有预先创建的预制件及其所有游戏对象。
如何修复此特定错误并在点击exit to main menu
时重新启动游戏
退出菜单按钮:
void OnMouseDown() {
PauseGame.isPause=false;
Application.LoadLevel("MainMenu");
}
播放按钮:
void OnMouseDown() {
spriteRenderer.sprite = s2;
Application.LoadLevel("Game");
}
我有三个预制件:
炮塔:
using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour {
// Use this for initialization
double LastBallTime=0.0;
double LastTurretMovingTime=0.0;
public double spawnballTime=1.5;
public double StartTurretMovingTime=35.0;
public double BallCount=2;
public bool IsRed=false;
public GameObject BallPrefab;
public GameObject RedBallPrefab;
Vector2 v ;
void Start () {
}
// Update is called once per frame
void Update () {
if (Time.time > LastBallTime + spawnballTime) {
LastBallTime=Time.time;
int rand= Random.Range(1,6);
if(rand>=1 && rand<=4)
Instantiate(BallPrefab, transform.position, transform.rotation) ;
else{
Instantiate(RedBallPrefab, transform.position, transform.rotation) ;
}
}
if (Time.time > LastTurretMovingTime + StartTurretMovingTime) {
LastTurretMovingTime=Time.time;
int x = Random.Range (-2,3);
int y = Random.Range (-3,3);
v = new Vector3 (x, y,0);
gameObject.transform.position=v;
}
}
void OnBecameInvisible ()
{
Destroy(gameObject);
}
}
BALL:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Ball : MonoBehaviour {
// Use this for initialization
public static double Killed=0;
public Text ScoreTextPrefab;
GameObject ParentCanvas;
void Start () {
ParentCanvas = GameObject.Find ("Canvas");
}
void OnMouseDown() {
if (!PauseGame.isPause) {
Text t = Instantiate (ScoreTextPrefab, transform.position, transform.rotation) as Text;
t.transform.SetParent (ParentCanvas.transform, false);
if (gameObject.name.Contains ("RedBall")) {
t.text = "O o P s";
RedKilled++;
} else {
Killed++;
}
Object.Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
}
void OnBecameInvisible ()
{
Destroy(gameObject);
}
}
TurretBall经理希望被分配给游戏场景的主摄像头:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TurretBallManager : MonoBehaviour {
// Use this for initialization
public GameObject TurretPrefab;
public double turretSpawnTime=35;
public double LastTurretTime=0;
public double MaxTurret=10;
int currentTurretCount=1;
public Text Killedtxt;
Vector2 v;
void Start () {
v = new Vector2(0,2);
Instantiate (TurretPrefab, v, Quaternion.identity);
}
void CreateTurret()
{
if (Time.time > LastTurretTime + turretSpawnTime) {
LastTurretTime=Time.time;
currentTurretCount++;
if(currentTurretCount>1)
v.x=TurretPrefab.transform.position.x-currentTurretCount/2;
if(currentTurretCount<=MaxTurret){
Instantiate(TurretPrefab,v,Quaternion.identity);
}
}
}
// Update is called once per frame
void Update () {
Killedtxt.text = "Kill : " + Ball.Killed;
CreateTurret ();
}
}