# Spawning a prefab at a specific point in a procedurally generated map

So I am new to Unity and I was working through the tutorial on procedural cave generation (this one)
and I’ve made it to the end. I would like to expand this into a small game where the player goes through an infinite series of levels but to do that I need to add a script that will make the player spawn inside of an open area. I wrote this method to do that and it is called when the map is generated.

``````void SpawnPlayer()
{
for (int x = 0; x < width; x++)
{
spawnLocationX = x;
for (int y = 0; y < height; y++)
{
spawnLocationY = y;
if (map[x, y] == 0)
{

if (playerCount == 0)
{
playerClone = Instantiate(player, new Vector4(spawnLocationX, -4, spawnLocationY, 0), Quaternion.identity);
playerCount = 1;
Debug.Log("Spawn Player");
return;
}
if (playerCount == 1)
{
player.position = new Vector4(spawnLocationX, 50, spawnLocationY, 0);
Debug.Log("Moveplayer");
return;
}

}
}
}
}
``````

From my understanding this code should check every tile of the map, get the first x, y coordinates of the first open point and spawn the player there. Then the next time its called it should just move the already existing player prefab to the new open point.

However what is happening is the first time the map is generated the player spawns at (0, -4, 0) and every other time its called the player doesnt move at all. The debug that says moveplayer does get called when the player should be moved and I have run debugs that show the for loops are checking and returning the x,y values of open spaces.

I was wondering if anyone could explain how to get this to spawn the player in an appropriate location or if I’m going about this the completely wrong way.

Thanks!

The position of a game object is stored as a Vector3. You’re trying to assign a Vector4 to it, which the compiler will do by just dropping the w value of the Vector4. It looks like you’re confusing your arguments because of this. As it is, you’re assigning the x position of your player correctly. The y value is being assigned to either -4 on the first instance or 50 if you’re just moving an already existing one. Then the z position is being set to your spawnLocationY.

``````player.position = new Vector3(spawnLocationX, spawnLocationY, WhateverYourZValueIS);
``````

Should be what you’re looking for. I don’t know what the -4 and 50 are supposed to represent in your Vector4s, so I wasn’t able to take those into account.