- Home /
Question by
Thagod · Mar 22, 2020 at 10:47 PM ·
instantiatearraygenerate
GameObject generation with arrays
Hello. I want to generate a game object with an array but I don't know how to use the c# arrays to get my result. I've made it in Java but need some help in converting it to c#.
public GameObject WallToGen;
int[,] map = new int[9, 9];
// Start is called before the first frame update
void Start()
{
map[1,1] = 1;
for (i = 0; i < map.Length; i++)
{
for (int j = 0; j < map[i].Length; j++)
{
switch (map[i,j])
{
case 1:
Instantiate(WallToGen);
break;
default:
break;
}
}
}
}
Comment
What exactly are you trying to do to? Define an array and fill it with GameObjects or assign an array to a GameObject?
Answer by ShadyProductions · Mar 22, 2020 at 11:41 PM
Replace your loops with this:
for (int x=0; x < map.GetLength(0); x++)
{
for (int y = 0; y < map.GetLength(1); y++)
{
switch (map[x, y])
{
case 1:
Instantiate(WallToGen);
break;
default:
break;
}
}
}
Your answer

Follow this Question
Related Questions
Selecting the currently active gameobject in an array to move its values 0 Answers
Material[] Object reference not set when instantiating 2 Answers
conditional gameobject spawning from collision array help please? 1 Answer
MissingMethodException: System.Collections.Generic.List 1 Answer
Cannot Instantiate Object 1 Answer