无法销毁错误的Transform组件 [英] Can't destroy Transform component of error

查看:294
本文介绍了无法销毁错误的Transform组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Unity代码,可以使用c#脚本创建一个预制件的网格(共2个维度).更改尺寸"值后(现在通过编辑器),我希望在Unity使用OnValidate更新之前删除所有预制件. Unity似乎不想删除代表该空间的上一组对象,因为这些对象仍然可以在Unity层次结构窗格中访问:

I've got some Unity code that creates a grid (2^dimensions in total) of one prefab using a c# script. Upon changing the 'dimensions' value (via the editor for now), I'd like to see all the prefabs get deleted, before Unity updates using OnValidate. Unity does not seem to want to delete the previous set of objects representing the space, as these objects are still accessible in the Unity Hierarchy Pane:

无法统一破坏对象.错误提示:

Having trouble destroying objects in unity. the error says:

无法销毁'XXX'的Transform组件.如果您想 销毁游戏对象,请在游戏对象上调用销毁" 反而.不允许销毁转换组件."

"Can't destroy Transform component of 'XXX'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed."

(请参阅函数DeletePoints/GeneratePoints.调用图:OnValidate-> GeneratePoints(-> DeletePoints,-> GeneratePointsHelper)

(refer to function DeletePoints/GeneratePoints. Call Graph: 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);
        }
    }
}

推荐答案

您当前正在使用Destroy (transform.GetChild (0));销毁GameObject.

You are currently destroying the GameObjects with Destroy (transform.GetChild (0));.

问题是transform.GetChild返回 Transform ,您不能销毁Transform.使用最新版本的Unity,您会收到此错误:

The problem is that transform.GetChild returns a Transform and you cannot destroy a Transform. With the lastest version Unity, you will get this error:

无法销毁"GameObject"的Transform组件.如果你想 销毁游戏对象,请在游戏对象上调用销毁" 反而.不允许销毁转换组件.

Can't destroy Transform component of 'GameObject'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.

您需要从Transform访问GameObject,然后销毁它.您还需要在GetChild中使用i而不是0,因为Destroy在for循环中被调用,而这很可能是您要尝试的操作.

You need to access the GameObject from the Transform then destroy it. You also need to use i in the GetChild instead of 0 since Destroy is being called in the for loop and that's likely what you are trying to do.

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

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

I'd like to see all the prefabs get deleted, before Unity updates using OnValidate

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

Then call DeletePoints() in the first line of the void OnValidate(){} function.

这篇关于无法销毁错误的Transform组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆