Home>

I am making run games in Unity.
I would like to select 3 Prefabs at random and place them side by side in the z-axis direction so that they do not overlap.

First obstacle Prefab: (0, 0, 0)
Second: (0, 0, 3)
Third: (0, 0, 6)
It is an image like

Each prefab is

Parent object
└ 3D model A
└3D model B

It has a parent-child structure like

.

All prefabricated prefabs have the same z-axis size,
For your information, I would appreciate it if you could tell me if the z-axis size is different.

Error message

Although it works well up to randomly generating multiple obstacle Prefabs,
All objects will be created at the position (0, 0, 0).

Applicable source code
int count = 0;
    int max = 3;
   [SerializeField] GameObject [] objList;
    void SetStageObjects ()
    {
        if (count == max) return;
        int number = Random.Range (0, objList.Length);
        Vector3 position = new Vector3 (0, 0, 0);
        Quaternion q = new Quaternion ();
        q = Quaternion.identity;
        GameObject obj = Instantiate (objList [number], position, q);
        count ++;
    }
Tried

position.z + = obj.transform.localScale.z;
Was inserted before count ++ ;, but it did not move as expected.
No error message is displayed.

Supplemental information

Because we are beginners, please point out questions that are difficult to understand or lack information.
Nice to meet you.

  • Answer # 1

    Instantiate's second argument is fixed atVector3 position = new Vector3 (0, 0, 0);

    If z is shifted by 3 (fixed each time),
    Vector3 position = new Vector3 (0, 0, count * 3.0f);

    If you want to get the size, the parent doesn't know the size of the child, and if you are using a 3D model, the calculation is more cumbersome.
    (The scale value of Transform is something like "size ratio". "The size set in the inspector of the 3D model (such as fbx file) × Transform Scale value") If the two are different, the size of the appearance will change.If the two child objects are separated, it will be more complicated)
    So it would be easier to create and give a "class that describes the size".

    // Class to specify the size (attach to the prefab parent. Set the value from the inspector)
    public class PrefabSize: MonoBehaviour {
        public Vector3 size;
    }
    // Class to generate
    // Prepare the following class variable
    private Vector3 stagePosition;
    void SetStageObjects () {
            // Omitted
            // The position can be anything
            GameObject obj = Instantiate (objList [number], position, q);
            // Add the specified size to the previous position
            stagePosition + = obj.GetComponent<PrefabSize>(). size;
            obj.transform.position = stagePosition;
    }


    * By the way, it may appear misaligned depending on the local position of the child object. (For example, if "origin = center", it will be half off)
    Please arrange so that the edge of the child object comes to the local coordinates (0,0,0), or adjust the size value of PrefabSize.