Enemy AI not working after Game Build

Hello! I am having a problem with my unity game. The enemies won’t detect and attack the player after the game build but it works fine when I test it in the editor. Anyone know why that is happening?

The deadline for the game is tomorrow and it’s having me worry.

How are your enemies generated and how do they find the player? Post code examples, as it’s quite possible the result of your method changes when things are arranged in a built game.

For example if you spawn both the player and an enemy at the same time and the enemy FindObjectOfType’s for the player in Awake, there’s a chance the player will exist and a chance it won’t exist by that point. These outcomes can change between the editor and a built game.

I used trigger colliders as detection zones and attack ranges:

//If player is within enemy detection zone
if (detectionZone.detectedObjects.Count > 0)
{
//Calculate direction to target object
Vector2 direction = (detectionZone.detectedObjects[0].transform.position - transform.position).normalized;

//Plays audio in specified scene
if (currentScene.name == “7_GameScene2”)
{
FindObjectOfType().Play(“GoblinGrowl”); //Goblin audio
}
else
{
FindObjectOfType().Play(“GoblinGrowl”); //Goblin audio
}
//Move towards detected object
rb.AddForce(direction * moveSpeed * Time.deltaTime);
animator.SetBool(“isMoving”, true);
//Flips enemy to face player
Vector3 scale = transform.localScale;
if (player.transform.position.x > transform.position.x)
{
scale.x = Mathf.Abs(scale.x) * (flip ? -1 : 1);
transform.Translate(Time.deltaTime * 0, 0, 0);
}
else if (player.transform.position.x < transform.position.x)
{
scale.x = Mathf.Abs(scale.x) * -1 * (flip ? -1 : 1);
transform.Translate(Time.deltaTime * 0, 0, 0);
}
transform.localScale = scale;
//If player is within enemy attack range
if (attackRange.attackPlayer.Count > 0)
{
//Enemy Attack animation
animator.SetTrigger(“Attack”);
}
}
else
{
//Enemy is idle
animator.SetBool(“isMoving”, false);
}

8729841--1181178--detectionCode.jpg