Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity在更改场景时未正确加载脚本_C#_Unity3d 2dtools - Fatal编程技术网

C# Unity在更改场景时未正确加载脚本

C# Unity在更改场景时未正确加载脚本,c#,unity3d-2dtools,C#,Unity3d 2dtools,因此,我为我的学校项目(一个类似于unity Roguelike的游戏)创建了一个主菜单,它与主游戏的场景不同。因此,在主菜单的脚本中,我对按钮进行了编码,以切换到我的主游戏场景。这就是问题发生的地方,当我点击开始游戏时,我的一些游戏对象在墙上,当我到达下一关的出口时,我就死了。当我启动主菜单场景并通过“开始”按钮更改场景时,游戏将在第2天开始,而游戏本应在第1天开始 无论如何,我认为问题就在这里 Player.cs using UnityEngine; using System.Collect

因此,我为我的学校项目(一个类似于unity 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);
}
using UnityEngine;
using System.Collections;


public class UIControl : MonoBehaviour
{

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

}
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;
    }
}
    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);
        }
    }
UIControl.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);
}
using UnityEngine;
using System.Collections;


public class UIControl : MonoBehaviour
{

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

}
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;
    }
}
    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);
        }
    }
GameController.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);
}
using UnityEngine;
using System.Collections;


public class UIControl : MonoBehaviour
{

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

}
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;
    }
}
    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);
        }
    }
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine.UI;
公共类游戏控制器:单行为
{
专用板控制器板控制器;
私人名单敌人;
私有游戏对象级图像;
私有文本水平文本;
私人布尔设置游戏;
private int secondsUntilLevelStart=2;
私有int currentLevel=0;
无效唤醒()
{
if(Instance!=null&&Instance!=this)
{
立即销毁(游戏对象);
返回;
}
实例=此;
DontDestroyOnLoad(游戏对象);
boardController=GetComponent();
敌人=新列表();
}
void Start()
{
初始化名称();
}
私有无效初始值设定名称()
{
setingupgame=true;
levelImage=GameObject.Find(“levelImage”);
levelText=GameObject.Find(“Level Text”).GetComponent();
levelText.text=“天”+当前级别;
levelImage.SetActive(真);
敌人。清除();
boardController.SetupLevel(当前级别);
调用(“DisableLevelImage”,SecondSunTilllevelStart);
}
私有void DisableLevelImage()
{
levelImage.SetActive(假);
setingupgame=false;
isPlayerTurn=真;
arenemiesmoving=假;
}
私有void onlevelwasload(int levelLoaded)
{
currentLevel++;
初始化名称();
}
公众假期
{
isPlayerTurn=false;
SoundController.Instance.music.Stop();
SoundController.Instance.PlaySingle(gameOverSound);
levelText.text=“您在“+currentLevel+”天后饿了……”;
levelImage.SetActive(真);
启用=错误;
}
}
BoardController.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);
}
using UnityEngine;
using System.Collections;


public class UIControl : MonoBehaviour
{

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

}
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;
    }
}
    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);
        }
    }
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类BoardController:MonoBehavior
{
公共int列;
公共int行;
公共游戏对象[]层;
公共游戏对象[]外墙;
公共游戏对象和障碍;
公共游戏对象[]食物数据项;
公开游戏对象[]敌人;
公共游戏对象退出;
私有转换游戏板;
私有列表障碍网格;
无效唤醒()
{
obstaclesGrid=新列表();
}
无效更新()
{
}
private void InitializeObjectalPositions()
{
obstaclesGrid.Clear();
对于(int x=2;xobstaclesGrid.Count)
{
obstaclecont=obstaclesGrid.Count;
}
for(int index=0;index
这些是与这个问题有关的主要脚本,我的其他脚本与敌人和声音有关,所以我怀疑我能在那里找到任何问题

很抱歉,我对格式不熟悉。

对于以后的笔记,您可以