Unity在更改场景时无法正确加载脚本

时间:2016-02-17 04:46:17

标签: c# unity3d-2dtools

所以我为我的学校项目(团结Roguelike游戏)创建了一个主菜单,这是与主游戏不同的场景。因此,在主菜单的脚本中,我将按钮编码为更改为我的主要游戏场景。这就是问题发生的地方,当我点击开始游戏时,我的一些游戏对象都在墙上,当我到达下一级别的出口时,我就死了。当我启动主菜单场景并通过我的开始按钮更改场景时,游戏将在第2天开始,此时它应该从第1天开始。

无论如何,这里是我认为问题所在的地方

Player.cs

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


   public class Player : MovingObject
   {

       public Text healthText;
       public AudioClip movementSound1;
       public AudioClip movementSound2;
       public AudioClip chopSound1;
       public AudioClip chopSound2;
       public AudioClip fruitSound1;
       public AudioClip fruitSound2;
       public AudioClip sodaSound1;
       public AudioClip sodaSound2;


       private Animator animator;
       private int playerHealth;
       private int attackPower = 1;
       private int healthPerFruit = 5;
       private int healthPerSoda = 10;
       private int secondsUntilNextLevel = 1;`


private void OnDisable()
{
    GameController.Instance.playerCurrentHealth = playerHealth;
}

void Update()
{
    if (!GameController.Instance.isPlayerTurn)
    {
        return;
    }

    CheckIfGameOver();

private void OnTriggerEnter2D(Collider2D objectPlayerCollidedWith)
{
    if (objectPlayerCollidedWith.tag == "Exit")
    {
        Invoke("LoadNewLevel", secondsUntilNextLevel);
        enabled = false;
    }
}

private void LoadNewLevel()
{
    Application.LoadLevel(Application.loadedLevel);
}

UIControl.cs

using UnityEngine;
using System.Collections;


public class UIControl : MonoBehaviour
{

    public void LoadScene(string loadName)
    {
        Application.LoadLevel(loadName);
    }

}

GameController.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{

    private BoardController boardController;
    private List<Enemy> enemies;
    private GameObject levelImage;
    private Text levelText;
    private bool settingUpGame;
    private int secondsUntilLevelStart = 2;
    private int currentLevel = 0;

    void Awake()
    {
        if (Instance != null && Instance != this)
        {
            DestroyImmediate(gameObject);
            return;
        }

        Instance = this;
        DontDestroyOnLoad(gameObject);
        boardController = GetComponent<BoardController>();
        enemies = new List<Enemy>();
    }

    void Start() 
    {
        InitializeGame();
    }

    private void InitializeGame()
    {
        settingUpGame = true;
        levelImage = GameObject.Find("Level Image");
        levelText = GameObject.Find("Level Text").GetComponent<Text>();
        levelText.text = "Day " + currentLevel;
        levelImage.SetActive(true);
        enemies.Clear();
        boardController.SetupLevel(currentLevel);
        Invoke("DisableLevelImage", secondsUntilLevelStart);
    }

    private void DisableLevelImage()
    {
        levelImage.SetActive(false);
        settingUpGame = false;
        isPlayerTurn = true;
        areEnemiesMoving = false;
    }

    private void OnLevelWasLoaded(int levelLoaded)
    {
        currentLevel++;
        InitializeGame();
    }




    public void GameOver()
    {
        isPlayerTurn = false;
        SoundController.Instance.music.Stop();
        SoundController.Instance.PlaySingle(gameOverSound);
        levelText.text = "You starved after " + currentLevel + " days...";
        levelImage.SetActive(true);
        enabled = false;
    }
}

BoardController.cs

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class BoardController : MonoBehaviour
    {

        public int columns;
        public int rows;

        public GameObject[] floors;
        public GameObject[] outerWalls;
        public GameObject[] wallObstacles;
        public GameObject[] foodItems;
        public GameObject[] enemies;
        public GameObject exit;

        private Transform gameBoard;
        private List<Vector3> obstaclesGrid;

        void Awake()
        {
            obstaclesGrid = new List<Vector3>();

        }


        void Update()
        {

        }

        private void InitializeObstaclePositions()
        {
            obstaclesGrid.Clear();

            for(int x = 2; x < columns - 2; x++)
            {
                for(int y = 2; y < rows - 2; y++)
                {
                    obstaclesGrid.Add(new Vector3(x, y, 0f));
                }
            }
        }

        private void SetupGameBoard()
        {
            gameBoard = new GameObject("Game Board").transform;

            for (int x = 0; x < columns; x++)
            {
                for (int y = 0; y < rows; y++)
                {

                    GameObject selectedTile;
                    if (x == 0 || y == 0 || x == columns - 1 || y == rows - 1)
                    {
                        selectedTile = outerWalls[Random.Range(0, outerWalls.Length)];
                    }
                    else
                    {
                        selectedTile = floors[Random.Range(0, floors.Length)];
                    }
                    GameObject floorTile = (GameObject)Instantiate(selectedTile, new Vector3(x, y, 0f), Quaternion.identity);
                    floorTile.transform.SetParent(gameBoard);
                }
            }
        }

        private void SetRandomObstaclesOnGrid(GameObject[] obstaclesArray, int minimum, int maximum)
        {
            int obstacleCount = Random.Range(minimum, maximum + 1);

            if(obstacleCount > obstaclesGrid.Count)
            {
                obstacleCount = obstaclesGrid.Count;
            }

            for(int index = 0; index < obstacleCount; index++)
            {

                GameObject selectedObstacle = obstaclesArray[Random.Range(0, obstaclesArray.Length)];
                Instantiate(selectedObstacle, SelectGridPosition(), Quaternion.identity);
            }
        }

        private Vector3 SelectGridPosition()
        {
            int randomIndex = Random.Range(0, obstaclesGrid.Count);
            Vector3 randomPosition = obstaclesGrid[randomIndex];
            obstaclesGrid.RemoveAt(randomIndex);
            return randomPosition;
        }

        public void SetupLevel(int currentLevel)
        {
            InitializeObstaclePositions();
            SetupGameBoard();
            SetRandomObstaclesOnGrid(wallObstacles, 3, 9);
            SetRandomObstaclesOnGrid(foodItems, 1, 5);
            int enemyCount = (int)Mathf.Log(currentLevel, 2);
            SetRandomObstaclesOnGrid(enemies, enemyCount, enemyCount);
            Instantiate(exit, new Vector3(columns - 2, rows - 2, 0f), Quaternion.identity);
        }
    }

这些是与此问题有关的主要脚本,我的其他脚本与敌人和声音有关,因此我怀疑我可以在那里找到任何问题。

我为格式化道歉,我是新来的。

0 个答案:

没有答案