Exemple #1
0
    private void OnLevelWasLoaded(int level)
    {
        if (sceneIndex != SceneManager.GetActiveScene().buildIndex)
        {
            if (sceneIndex == 0 && SceneManager.GetActiveScene().buildIndex == 1)
            {
                audioSource.clip = clip1;
                audioSource.Play();
            }
            else if (sceneIndex != 0 && SceneManager.GetActiveScene().buildIndex == 0)
            {
                audioSource.clip = clip2;
                audioSource.Play();
            }
            sceneIndex = SceneManager.GetActiveScene().buildIndex;
        }

        if (level == levelIndex + 1)
        {
            foreach (var ghost in GameObject.FindGameObjectsWithTag("Ghost"))
            {
                Destroy(ghost);
            }
            Destroy(GameObject.FindGameObjectWithTag("Player"));

            for (int i = 0; i < GhostManager.levelData[levelIndex].Count; i++)
            {
                GhostManager.GhostData ghost = GhostManager.levelData[levelIndex][i];

                if (i != PlayerController.RobotIndex && ghost.record.Count != 0)
                {
                    GameObject newGhost = Instantiate(Resources.Load("Ghost")) as GameObject;
                    newGhost.transform.position = levelSpawns[levelIndex];
                    newGhost.GetComponent <GhostController>().ghostIndex = i;
                }
            }
            // Load the keys
            foreach (var ghost in GameObject.FindGameObjectsWithTag("Ghost"))
            {
                try
                {
                    GhostController controller = ghost.GetComponent <GhostController>();
                    controller.LoadKeys(GhostManager.GetGhostData(levelIndex, controller.ghostIndex));
                    controller.Restart();
                }
                catch
                {
                    print("Error in setting records for all ghosts.");
                }
            }
            // Load the player
            GameObject player = Instantiate(Resources.Load("Player")) as GameObject;
            player.transform.position = levelSpawns[levelIndex];

            // Check if there are multiple players and delete all others
            GameObject[] tmpPlayers = GameObject.FindGameObjectsWithTag("Player");
            if (tmpPlayers.Length > 1)
            {
                foreach (var p in tmpPlayers)
                {
                    if (p != player)
                    {
                        Destroy(p);
                    }
                }
            }
        }
    }