Enemy dies in editor mode but not in build
Hello. I am using one enemy that has a patrol variation. Normally, the enemy stands still and chases the player when the player comes in radius. I made a version of the enemy that patrols back and forth. In editor mode, everything works perfectly. But when I build and run it the idle enemy dies, but the patrol one is invincible. The knock back works but he won't die. I am very frustrated and confused as to what is happening.
How my project is constructed is the enemy inherits from an Enemy script for health:
private void Awake()
{
health = maxHealth.initialValue;
}
private void Start()
{
health = maxHealth.initialValue;
}
private void TakeDamage(float damage)
{
health -= damage;
if (health <= 0)
{
soundSource.PlayOneShot(deathSound);
DeathEffect();
MakeLoot();
this.gameObject.SetActive(false);
}
}
Then I have a scriptable object that is a float value of 2 that is the health of the enemy. The script 'Wolf' inherits from the Enemy script, and then the paroling version inherits from the 'Wolf' script. They both have the same scritable object. Here is the one for the Wolf enemy:
Here is the patrol variation in the inspector:
What would cause this to work in the editor but not on build? I don't get any errors related to my enemies.
Answer by streeetwalker · Apr 01, 2020 at 12:30 PM
@nilesmac, that is not enough information to go on to give you any meaningful advice. We'd need to see the full scripts for all objects involved.
And what do you mean it 'works in Editor Mode' - you are running the scripts using [ExecuteInEditMode] ?
By editor mode, I mean when you are actually able to edit the game and create it. As opposed to running 'Build and run'. As I am making the game, and hitting play to test it out, everything works fine. Once I Build and Run it, my patrol enemy can not die. I have attached all three files for the Enemy, Wolf, and PatrolWolf.
Here is the third one for the Patrol version of the enemy. The site would not like me attach a third file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PatrolWolf : Wolf
{
//By not having start you use the base classes start method
public Transform[] path;
public int currentPoint;
public Transform currentGoal;
public float roundingDistance;
public override void CheckDistance()
{
if (Vector3.Distance(target.position,
transform.position) <= chaseRadius
&& Vector3.Distance(target.position,
transform.position) > attackRadius)
{
if (currentState == EnemyState.idle || currentState == EnemyState.walk
&& currentState != EnemyState.stagger)
{
Vector3 temp = Vector3.$$anonymous$$oveTowards(transform.position,
target.position,
moveSpeed * Time.deltaTime);
changeAnim(temp - transform.position);
myRigidbody.$$anonymous$$ovePosition(temp);
//ChangeState(EnemyState.walk);
anim.SetBool("wakeUp", true);
}
}
else if (Vector3.Distance(target.position,
transform.position) > chaseRadius)
{
if (Vector3.Distance(transform.position,
path[currentPoint].position) > roundingDistance)
{
Vector3 temp = Vector3.$$anonymous$$oveTowards(transform.position,
path[currentPoint].position,
moveSpeed * Time.deltaTime);
changeAnim(temp - transform.position);
myRigidbody.$$anonymous$$ovePosition(temp);
}
else
{
ChangeGoal();
}
}
}
private void ChangeGoal()
{
if (currentPoint == path.Length - 1)
{
currentPoint = 0;
currentGoal = path[0];
}
else
{
currentPoint++;
currentGoal = path[currentPoint];
}
}
}
@nilesmac, yes, we need to know that too: So it doesn't work in the build. What platform are you testing it on - what's the build target?
I am running it and building it on/for windows PC.