I have a grid based movement system. The for-loop runs through every tile and checks it is occypied (first if) and the attitude (second if). The problem is: this code is working perfectly in editor, but in build the second if just isn't being executed. I put it first in the script execution order so it doesn't conflict with other scripts. I've been trying to solve it for 3 days. Just don't know what else I could do. Thanks :)
void Start()
{
WalkGrid = new WalkGrid(width, height, referenceObject1, referenceObject2);
tileSize = Math.Abs(referenceObject1.transform.position.x - referenceObject2.transform.position.x);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 0, j * tileSize), Vector3.up, out RaycastHit hit2, tileSize * 10) && hit2.collider.CompareTag("Obstacle"))
{
WalkGrid.gridArray[i, j].isOccupied = true;
}
if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 9, j * tileSize), Vector3.down * 10, out RaycastHit hit, 10))
{
WalkGrid.gridArray[i, j].attitude = hit.point.y;
}
}
}
}
- Script execution order
- Put it in 2 different for loops
- Used Awake instead of start (was even worse)
I have a grid based movement system. The for-loop runs through every tile and checks it is occypied (first if) and the attitude (second if). The problem is: this code is working perfectly in editor, but in build the second if just isn't being executed. I put it first in the script execution order so it doesn't conflict with other scripts. I've been trying to solve it for 3 days. Just don't know what else I could do. Thanks :)
void Start()
{
WalkGrid = new WalkGrid(width, height, referenceObject1, referenceObject2);
tileSize = Math.Abs(referenceObject1.transform.position.x - referenceObject2.transform.position.x);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 0, j * tileSize), Vector3.up, out RaycastHit hit2, tileSize * 10) && hit2.collider.CompareTag("Obstacle"))
{
WalkGrid.gridArray[i, j].isOccupied = true;
}
if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 9, j * tileSize), Vector3.down * 10, out RaycastHit hit, 10))
{
WalkGrid.gridArray[i, j].attitude = hit.point.y;
}
}
}
}
- Script execution order
- Put it in 2 different for loops
- Used Awake instead of start (was even worse)
2 Answers
Reset to default 0Your issue is most likely related to your attempt to do physics interactions before any physics update right at the start of the game. You could try calling Physics.SyncTransforms at the beginning of Start. Though waiting at least one frame may still be necessary. This could be done by turning Start into a coroutine.
IEnumerator Start()
{
yield return null;
Physics.SyncTransforms();
WalkGrid = new WalkGrid(width, height, referenceObject1, referenceObject2);
// [ ... ]
The solution is quiet strange. I just reimported the model with mesh collider and it worked! Maybe it was a bug while importing although the issue was only in the build
Used Awake instead of start (was even worse)
.. how was it worse? – derHugo Commented Feb 1 at 17:03if
is failing? As mentioned above do some Debugging .. hard to tell from here with the information we have – derHugo Commented Feb 1 at 17:04