简体   繁体   English

为什么我的脚本会破坏资产? 统一

[英]Why is my script destroying assets? Unity

In my script I instantiate obstacles (prefabs are stored at prefabObstacles array) at transforms position ( emptyTransforms array).在我的剧本我实例化障碍(预制件存储在prefabObstacles阵列)在变换位置( emptyTransforms阵列)。 This script is attached to gameobject which is segment of the world.此脚本附加到gameobject ,这是世界的一部分。 This segment is dynamically instantiated and destroyed.该段是动态实例化和销毁的。

public class ObstaclePlacer : MonoBehaviour
{
    public int obstaclesAmmount;
    public GameObject[] prefabObstacles;
    public Transform[] emptyTransforms;
    public GameObject[] obstacles;

    void Start()
    {
        GenerateObstacles();
    }

    void GenerateObstacles()
    {
        for(int i = 0; i < Random.Range(0,obstaclesAmmount); i++)
        {
            GameObject objToSpawn = prefabObstacles[Random.Range(0, prefabObstacles.Length)];
            obstacles[i] = Instantiate(objToSpawn, (emptyTransforms[Random.Range(0, emptyTransforms.Length)].position+new Vector3(0.0f, 1.0f)), Quaternion.identity) as GameObject;
        }
    }

    void OnDestroy()
    {
        foreach(GameObject item in obstacles)
        {
            Destroy(item);
        }
    } 
}

This bug is occurring:这个错误正在发生:

Destroying assets is not permitted to avoid data loss.不允许破坏资产以避免数据丢失。
If you really want to remove an asset use DestroyImmediate (theObject, true);如果您真的想删除资产,请使用DestroyImmediate (theObject, true);
UnityEngine.Object:Destroy(Object) UnityEngine.Object:销毁(对象)
ObstaclePlacer:OnDestroy() (at Assets/Scripts/ObstaclePlacer.cs:37) ObstaclePlacer:OnDestroy()(在 Assets/Scripts/ObstaclePlacer.cs:37)

It only works if obstacles array size is fixed.仅当障碍物阵列大小固定时才有效。

Do you know how can I fix that?你知道我该如何解决吗?
If you need more details just ask me.如果您需要更多详细信息,请询问我。 Thanks for help!感谢帮助!

I think the main disconnect here is understanding how arrays work in C#.我认为这里的主要脱节是理解数组在 C# 中的工作方式。 You must declare the length of the array when you initialize it, this is different than other collections such as, generic lists, which allow you to dynamically grow or shrink the list as needed.您必须在初始化数组时声明数组的长度,这与其他集合(例如泛型列表)不同,后者允许您根据需要动态增长或缩小列表。 There are deeper differences between various collections and arrays, which you will want to understand before you decide which is best for your application.各种集合和数组之间存在更深层次的差异,在决定哪个最适合您的应用程序之前,您需要了解这些差异。

This example shows how to declare and initialize an instance of both an array and generic list of type GameObjects in the context of your Behavior Script, obviously you'd want to use one or the other in your actual script.此示例显示了如何在行为脚本的上下文中声明和初始化类型为 GameObjects 的数组和泛型列表的实例,显然您希望在实际脚本中使用其中一个。

class ObstaclePlacer : MonoBehaviour
{
    public int obstaclesAmmount;
    public GameObject [] obstaclesArray;
    public List<GameObject> obstaclesList;

    public void Start ()
    {
        var random = new System.Random();
        //Get the desired size of your array.
        obstaclesAmmount = random.Next(0, 100);
        //Initialize an array with the size of your random number
        obstaclesArray = new GameObject [obstaclesAmmount];
        //Or use a list which allows your to dynamically add elements later.
        obstaclesList = new List<GameObject> ();

        GenerateObstacles ();
    }

    void GenerateObstacles ()
    {


        for (int i = 0; i < obstaclesAmmount; i++) {

            var gameObject = new GameObject();

            obstaclesArray[i] = gameObject;

            obstaclesList.Add(gameObject);
        }
    }

    void OnDestroy ()
    {
        foreach (GameObject item in obstaclesArray) {
            //Destroy (item);
            Console.WriteLine ("Destroy (item); in obstaclesArray");
        }

        foreach (GameObject item in obstaclesList) {
            //Destroy (item);
            Console.WriteLine ("Destroy (item); in obstaclesList");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM