// Use this for initialization void Start() { timeDisplay = gameObject.GetComponent<Text>(); //find text component if (GameObject.Find("GameScoreController") != null) //The timer SHOULD have been saved and brought over from the previous level { GameObject timerObject = GameObject.Find("GameScoreController"); scoreScriptRef = timerObject.GetComponent<gameScoreScript>(); scoreScriptRef.getSavedTime(); timeDisplay.text = "Time: " + scoreScriptRef.displayTime(); DestroyObject(timerObject); //We must destroy the timer/GameScoreController object or else it wll carry over to the next level and mutiple ones might be present } else { timeDisplay.text = "Error: No 'GameScoreController' found."; timeDisplay.fontSize = 20; } }
void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player") //only activate if a player object runs into the goal { //other.gameObject.SetActiveRecursively(false); DestroyObject(other.gameObject); GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player"); Debug.Log (listOfPlayers.Length); if (listOfPlayers.Length-1 == 0) { //Get the timer script from the game object, stop it, then save the time scoreScriptRef = gameScoreController.GetComponent<gameScoreScript>(); scoreScriptRef.stopTimer(); scoreScriptRef.saveTime(); NextLevel(levelIndex); NextLevel(levelSceneName); } } }