无法破坏转换组件的错误

时间:2016-11-15 05:07:35

标签: c# unity3d unity5

我有一些Unity代码使用c#脚本创建一个预制件的网格(总共2个维度)。在更改“尺寸”值(目前通过编辑器)后,我想在使用OnValidate进行Unity更新之前看到所有预制件都被删除。 Unity似乎不想删除表示空间的上一组对象,因为这些对象仍然可以在Unity Hierarchy窗格中访问:

无法一致地摧毁物体。错误说:

  

“无法销毁'XXX'的变换组件。如果你愿意的话   销毁游戏对象,请在游戏对象上调用“Destroy”   代替。不允许销毁转换组件。“

(参考函数DeletePoints / GeneratePoints。调用图:OnValidate - > GeneratePoints( - > DeletePoints, - > GeneratePointsHelper)

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

public class BinarySpacePointGenerator : MonoBehaviour {
    private const int UNITY_DRAW_SPACE_DIMENSIONALITY = 3;
    /**
     * These values denote spacings for the three dimensional space between binary points.
     */
    public float xoff, yoff, zoff;
    public float scale;
    public Transform pointPrefab;
    /**
     *  The current dimensionality of binary space to be displayed.
     */
    public int dimensions;

    /* 
     *  The container of points that represent our B space. 
     */
    private List<Transform> points;

    // Use this for initialization
    void Start () {
        xoff = 1.0f;
        yoff = 1.0f;
        zoff = 1.0f;
        scale = 0.25f;
        dimensions = 2;
        points = new List<Transform> ();
        GeneratePoints ();
    }

    void OnValidate() {
        /* ensure dimensionality */
        /* TODO: set up a B^0 space. */
        if (dimensions < 1) {
            dimensions = 1;
        }
        if (dimensions >= 13) {
            dimensions = 12;
        }
        /* ensure that our spacing sizes are valid */
        if (xoff <= 0.0f) {
            xoff = 1.0f;
        }
        if (yoff <= 0.0f) {
            yoff = 1.0f;
        }
        if (zoff <= 0.0f) {
            zoff = 1.0f;
        }
        if (scale <= 0.0f) {
            scale = 0.25f;
        }

        /* now that we've ensured the dimensionality, we can change the space */
        GeneratePoints ();
    }

    private void DeletePoints() {
        for (int i = 0; i < transform.childCount; i++) {
            Destroy (transform.GetChild (0));
        }
        points.RemoveRange(0, points.Count); /* pop off */
    }

    /**
     *  Instantiates the points field based on the value of dimensions at call-time.
     */ 
    private void GeneratePoints() {
        DeletePoints ();

        int[] vectorProto = new int[dimensions];
        for (int i = 0; i < dimensions; i++) {
            vectorProto [i] = 0;

        }
        GeneratePointsHelper(vectorProto, dimensions);
    }

    /** 
     * 
     * GeneratePointsHelper
     * 
     * Description: Recursively builds the binary space B^n.
     * 
     * Parameters:
     *      int[]   vector:  the proto-type of all higher dimensions for the current trail.
     *      int     n:  the number of dimensions left to traverse from this recursion step.
     * 
     * Recursion Termination/Description:  
     *      When n == 0, which means that we have created a unique vector.
     * 
     */
    private void GeneratePointsHelper(int[] vector, int n) {
        if (n == 0) {
            // use vector to set Sphere object
            var point = Instantiate(pointPrefab);
            Vector3 pointPosition = new Vector3 ();
            pointPosition.x = 0;
            pointPosition.y = 0;
            pointPosition.z = 0;
            for (int i = 0; i < dimensions; i++) {

                int d = (i / UNITY_DRAW_SPACE_DIMENSIONALITY);

                if ( i % UNITY_DRAW_SPACE_DIMENSIONALITY == 0) {
                    pointPosition.x += (xoff * vector[i] * Mathf.Pow(2, d));
                } else if (i % UNITY_DRAW_SPACE_DIMENSIONALITY == 1) {
                    pointPosition.y += (yoff * vector[i] * Mathf.Pow(2, d));
                } else if (i % UNITY_DRAW_SPACE_DIMENSIONALITY == 2) {
                    pointPosition.z += (zoff * vector[i] * Mathf.Pow(2, d));
                }
            }
            point.localPosition = pointPosition;
            point.localScale = new Vector3 (scale, scale, scale);
            point.parent = transform;
            points.Add (point);

        } else {
            vector[dimensions-n] = 0;
            GeneratePointsHelper (vector, n - 1);

            vector[dimensions-n] = 1;
            GeneratePointsHelper (vector, n - 1);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您目前正在使用Destroy (transform.GetChild (0));销毁GameObjects。

问题在于transform.GetChild会返回Transform而您无法销毁Transform。使用最新版本的Unity,您将收到此错误:

  

不能销毁&#39; GameObject&#39;的变换组件。如果你想   破坏游戏对象,请致电&#39; Destroy&#39;在游戏对象上   代替。不允许销毁转换组件。

您需要从Transform访问GameObject然后销毁它。您还需要在i而不是GetChild中使用0,因为在for循环中调用Destroy并且您可能正在尝试执行此操作

for (int i = 0; i < transform.childCount; i++)
{
    Destroy(transform.GetChild(i).gameObject);
}
  

我希望在Unity更新之前看到所有预制件都被删除   使用OnValidate

然后在DeletePoints()函数的第一行调用void OnValidate(){}