Raycast Not Detecting Collider Until After Game Loads
Hi, I'm making a 2D game that procedurally generates a dungeon. One of the things I have in it is mining nodes. When placing a mining node, I want to make sure that there isn't something with a collider already at that position. The problem I'm running into is that during the process of generating the dungeon, it places a node (that has a 2d box collider), then when placing another node at the same position, the check to see if a collider already exists in that spot doesn't find anything. However, when I run the code after the level has fully built (with a button press), it detects the collider without issue. So it seems like until the game is actually, the colliders don't exist. How do I make it detect colliders during the generation process?
I also tried placing an empty gameobject in the spot with a 2d box collider in it on the map so it's there before I run the game, and it detects a collider there when placing both nodes. So I'm pretty confident it's not creating the collider until after the procedural generation.
private void PlaceMiningNodes()
{
foreach (var position in wallPositions)
{
if (ProceduralRNG.Next(1000) < 30)
{
Vector3Int? nodePosition = GetNearestGroundPositionFromEdge((Vector3Int)position);
if (nodePosition != null)
{
GameObject miningNode = Instantiate(miningNodePrefab);
miningNode.transform.position = floorTilemap.GetCellCenterWorld((Vector3Int)nodePosition);
miningNode.transform.parent = miningNodesContainer.transform;
}
}
}
}
public bool CheckIfObjectExistsAtPosition(Vector3Int position)
{
//check if there is something at that position
RaycastHit2D hit = Physics2D.Raycast(floorTilemap.GetCellCenterWorld((Vector3Int)position), Vector2.up, 0f);
if (hit.collider != null)
{
return true;
}
return false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613055733im_/https://answers.unity.com/themes/thub/images/avi.jpg)