ask about two-dimensional array in Unity

Hi,

I have a question abot arry. I created an arry:

Then in start:

it report error of this line: “floors[x,y] =(GameObject)Instantiate(floors, Vector3 (forX, 0, forZ), Quaternion.identity);” and the error is:
" error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected"
I wonder if someone know why that happened.
Thanks!

It may help if you pointed out what line that error was talking about.
Though I suspect it could be Vector3 needs to be new Vector3 if you are using C#.

Is this c#? because then you need to declare the vector as a new class

floors = new GameObject[20,20];
float forX=-200f;
float forZ=-200f;
for (int x = 0; x < 20; x++) {
for (int y = 0; y < 20; y++) {
floors[x,y] =(GameObject)Instantiate(floors, new Vector3 (forX, 0, forZ), Quaternion.identity);
forX+= 20f;
}
forZ+= 20f;
}

why are you instantiating the array? you need instantiate a prefab maybe?

This as well. You are assigning each element of the array to an instantiation of the array as a whole, and trying to cast 20 * 20 GameObjects into 1.

Edit:
I assume you meant to have a floor prefab such as

public GameObject floor; //Assign prefab here
public GameObject[,] floors; // This is the 2D array being generated

void Start()
{
floors = new GameObject[20,20]; // Initialising the array of 20 * 20 gameobjects
float forX=-200f;
float forZ=-200f;

for (int x = 0; x < 20; x++)
{
	for (int y = 0; y < 20; y++)
	{
               // Creating the game objects within the array at the specified indexes
		floors[x,y] =(GameObject)Instantiate(floors, new Vector3 (forX, 0, forZ), Quaternion.identity); 
		forX+= 20f;
	}
	forZ+= 20f;
}
}

You have to assign the prefab to floor and not floors

Hi ivkoni,
I think it is the problem you said and I only changed the code of the following line:

but it still report error of this line:
“The name `floor’ does not exist in the current context”

“floor” is my prefab, I draged it into the “Floors”. Do you know why it still report the error?
Thanks!

Hi Ntero,
Thank you. The problem is that I should Instantiate the prefab~~

Hi CorruptedHeart,
As posted, the problem is that I didn’t Instantiate right. there is still problem though. Thank you!

Hi,
I tried the method that you told me. One odd thing is that there is no “floor” shown in the Inspector view~~

is it declared like this exactly?

public GameObject floor;

yes, the same