2016-11-15 18 views
1

私は、C#スクリプトを使用して1つのプレハブのグリッド(合計2次元)を作成するUnityコードを取得しています。 'dimensions'の値を変更すると(エディタ経由で)、OnValidateを使用してUnityアップデートを行う前に、すべてのプレハブが削除されるのを見たいと思います。これらのオブジェクトはユニティ階層ペインでまだアクセス可能であるため、ユニティはスペースを表す以前のオブジェクトセットを削除したくないようです。エラーの変換コンポーネントを破棄できません

1つのオブジェクトを破壊することに問題があります。エラーは言う:

"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)

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

このスレッドでは、同じ問題について話しているようです:https://forum.unity3d.com/threads/onvalidate-and-destroying-objects.258782/ – CaTs

答えて

4

あなたは現在でゲームオブジェクトを破壊しています。Destroy (transform.GetChild (0));

問題はtransform.GetChildTransformを返し、あなたがTransformを破壊することができないということである最新のバージョンUnityと、このエラーが表示されます:

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からアクセスして破壊する必要があります。 Destroyがforループで呼び出されているので、0の代わりにGetChildiを使用する必要があります。これは実行しようとしている可能性が高いです。

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

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

そしてvoid OnValidate(){}関数の1行目にDeletePoints()を呼び出します。

+0

"私はすべてのプレハブが削除されるのを見たいのですが"あなたが本当にこの問題に焦点を当てるべきものです。むしろ、これはGameObjectsを管理するためにUnity APIを悪用するという問題です。私が理解しているところでは、実際にはDestroy(子)ループの相対的な配置(「OnValidate」にループコードを置いているのか、 'OnValidate'に従属させているのか)のようには思えません。最終的に起こっていることは、間違ったオブジェクトや関数を使って、私のDestroy(child)ループを間違って "調理"してしまったと思います。これが間違っている場合は、私の理解を修正してください:) – Ian

関連する問題