- Home /
switching scenes by loadlevel, back to previous scene I got a problem
this is my test video link text
Hello~ I test loadlevel command.
I have two scene:
1>Unity-chan SecondaryAnimation
2>I wrote a scene -runGame
flow chart: SecondaryAnimation -by Click btn->runGame-game over->SecondaryAnimation
first switch SecondaryAnimation -->runGame
I change scene at video 18 sec.
runGame works perfectly.
second switch runGame-->SecondaryAnimation I change again in 35 sec. In video you can see SecondaryAnimation not work... only Click button can do loadlevel, but scene is not work successfully.
what should I do?
I use Unity 4.3 .
Thank you very much
↓ is part of code ↓
GameController.cs for runGame
using UnityEngine;
using System.Collections;
public class GameControlScript : MonoBehaviour {
float timeRemaining = 10; //Pre-earned time
float timeExtension = 3f; //time to extend by on collecting powerup
float timeDeduction = 2f; //time to reduce, on collecting the snag
float totalTimeElapsed = 0;
public float score=0f; //total score
public bool isGameOver = false;
public GUISkin skin;
// Use this for initialization
void Start () {
Time.timeScale = 1;
}
// Update is called once per frame
void Update () {
if(isGameOver)
{
return;
}
totalTimeElapsed += Time.deltaTime;
score = totalTimeElapsed*100;
timeRemaining -= Time.deltaTime;
if(timeRemaining <= 0)
{
isGameOver=true;
}
}
void OnGUI()
{
GUI.skin=skin;
if(!isGameOver)
{
GUI.Label(new Rect(10,10,Screen.width/3, Screen.height/3),"Time Life: " + ((int)timeRemaining).ToString());
GUI.Label(new Rect(Screen.width-(Screen.width/3), 10, Screen.width/3, Screen.height/3), "Score: "+((int)score).ToString());
//PlayerPrefs.SetString("score",""+(int)score);
}
else
{
Time.timeScale=0;
//set the timescale to zero so as to stop the game
//display the final score
GUI.Box(new Rect((Screen.width/4)-20, Screen.height/5, Screen.width/2, Screen.height/2), "GAME OVER\nYour Score: "+(int)score);
if(GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+3*Screen.height/10+10, Screen.width/2-20, Screen.height/10),"Exit Game"))
{
//Debug.Log("****************"+PlayerPrefs.GetString("score")+"*********************");
//Destroy(GameObject.Find("runroot"));
//Application.LoadLevel(0);
Application.LoadLevel ("SecondaryAnimation");
}
}
}
public void DonutCollected()
{
timeRemaining += timeExtension; //add time to the time remaining
}
public void OnionCollected()
{
timeRemaining -= timeDeduction; // deduct time
}
}
CameraController.cs from Unity-chan's MainCamera
void OnGUI()
{
if(showInstWindow){
GUI.Box(new Rect(Screen.width -210, Screen.height - 100, 200, 90), "Camera Operations");
GUI.Label(new Rect(Screen.width -200, Screen.height - 80, 200, 30),"RMB / Alt+LMB: Tumble");
GUI.Label(new Rect(Screen.width -200, Screen.height - 60, 200, 30),"MMB / Alt+Cmd+LMB: Track");
GUI.Label(new Rect(Screen.width -200, Screen.height - 40, 200, 30),"Wheel / 2 Fingers Swipe: Dolly");
}
if (Application.loadedLevelName.Equals ("SecondaryAnimation")) {
if (GUI.Button (new Rect (Screen.width / 2, 70, 100, 30), "Click"))
Application.LoadLevel ("runGame");
}
else {
if (GUI.Button (new Rect (Screen.width / 2, 70, 100, 30), "Click"))
Application.LoadLevel ("SecondaryAnimation");
}
}
I modify the code. It's work~! the problem in GameController.cs. I add Time.timeScale=0; that make scene can't work.