Search code examples
c#unity-game-enginetransformgameobject

Create empty object with transform information in Unity3D


My initial intention was to create transform objects in an array, because all I need are certain points in space, that hold information about the position and rotation. I discovered that this wasn't possible, so I then went to create an array of empty GameObjects:

public GameObject[] centers;

From these I hoped to get what I need. Which is this:

centers = new GameObject[length];
GameObject prevCenter = myHeadLink; // myHeadLink is an unrelevant GamObject
for(int i = 0; i < length; i++){
    Debug.Log(centers[i]);
    centers[i].transform.position = prevCenter.transform.position -
            prevCenter.transform.forward * prevCenter.transform.localScale.z * 1.5f;
    prevCenter = centers[i];
}

Unfortunately, that's not possible either, because all center[i] are null. Does new GameObject not create an empty GameObject with transform information?


Solution

  • Try to write:

    for (int i = 0; i < length; ++i)
      centers[i] = new GameObject();
    

    after:

    centers = new GameObject[length];