In the unity use process, many times will use the length indeterminate object array.
An indeterminate array of objects uses the code of the Declaration section.
However, as long as it is an array, you have to keep the object in an array of objects, you must know the number of objects to be stored, that is, you must know the length of the array
Use list, also can be implemented, this article does not repeat
Define private list<vector3> offsets = new list<vector3> ();
Offsets. ADD (transform.position); Add objects sequentially from 0
Offsets. RemoveAt (0); Remove an object that is numbered 0
In the actual use of the code, there are many cases in which the length of the array is dynamically changing, depending on the conditions, the different variables constantly changing, with my code as an example
To create a 4-face wall dynamically, you have to know the length and height of the object, the x,y,z of the scale of the empty object, to replace the height of the wall of the empty object.
The number of unit squares that make up the 4-face wall
float length = 0; float width = 0;
width = transform.localscale.z * transform.localscale.y;//(z+)
length = transform.localscale.x * transform.localscale.y;//(x+)
Once you know, create a new object array that corresponds to the number of unit squares of the corresponding walls of the wall object array
Wallgo1 = new gameobject[(int) width];
Wallgo3 = new gameobject[(int) width];
Wallgo2 = new gameobject[(int) length];
Wallgo4 = new gameobject[(int) length];
Assign an empty value to each object, or you will get an error (please take the actual situation as the main)
for (int i = 0; i < width;i++)
{
wallgo1[i] = null; Wallgo3[i] = null;
} for
(int i = 0; i < length; i++)
{
wallgo2[i] = null; Wallgo4[i] = null;
} //
Then, to call all the unit squares on one side of the wall, call the 4 corresponding array of wall objects directly
Please refer to the article in combination
how the "unity& Elf" obtains objects & dynamically generated walls through known coordinates
So how do you create it?
Statement
Private gameobject[] Wallgo1 = null;//Wall array wallgo1
private gameobject[] wallgo2 = null;
Private gameobject[] Wallgo3 = null;
Private gameobject[] Wallgo4 = null;
Assigning values
void Wallgoarray ()
{
float length =0; float width = 0;
width = transform.localscale.z * transform.localscale.y;//(z+)
length = transform.localscale.x * transform.localscale.y;//(x+)
wallgo1 = new gameobject[(int) width];
Wallgo3 = new gameobject[(int) width];
Wallgo2 = new gameobject[(int) length];
Wallgo4 = new gameobject[(int) length];
for (int i = 0; i < width;i++)
{
wallgo1[i] = null; Wallgo3[i] = null;
} for
(int i = 0; i < length; i++)
{
wallgo2[i] = null; Wallgo4[i] = null;
} //
}//
Call
An object array can be invoked only if the object array has an object and has a certain length.
You can call these 4 object arrays in other functions. (It is important to note that the 4 objects array, first declared, after the assignment, the specified length, and the length of each object assignment, even if it is null also line)
The above is the use of creating an array of objects with an indeterminate length.