- Home /
My game crashes when the enemy makes contact with the player
I'm trying to make the the enemies in my scene damage my player when they come within a certain range of the player, but every time the the enemy comes within this range unity crashes and is frozen to my screen, i am unable to close the window forcing me to restart my computer. I have built the game but it still crashes however a window comes up telling me that the program is not responding and i am able to close the program. I have tried using a collision to damage the player but i had the same problem.
Here is my code:
(Character's script)
using UnityEngine;
using System.Collections;
public class CharacterSetup : MonoBehaviour {
public float characterHealth;
public float amountOfDamage;
public bool isBeingAttacked;
public CharacterShoot CharShoot;
public CharacterMovement CharMove;
public CapsuleCollider playerCol;
public MeleEnemyAi[] MeleEnemyAiArray;
public GameObject [] MeleEnemies;
// Use this for initialization
void Start () {
playerCol = GetComponent<CapsuleCollider> ();
CharMove = GetComponent<CharacterMovement> ();
CharShoot = GetComponent<CharacterShoot> ();
CharMove.enabled = true;
CharShoot.enabled = true;
this.enabled = true;
}
// Update is called once per frame
void Update () {
MeleEnemies = GameObject.FindGameObjectsWithTag ("Mele Enemy");
MeleEnemyAiArray = new MeleEnemyAi[MeleEnemies.Length];
for (int i = 0; i < MeleEnemies.Length; i++) {
MeleEnemyAiArray [i] = MeleEnemies [i].GetComponent<MeleEnemyAi> ();
}
while (isBeingAttacked == true) {
Deducthealth ();
}
if (characterHealth <= 0) {
Die ();
}
}
void Deducthealth(){
characterHealth -= amountOfDamage;
}
void Die(){
print ("You have been killed!!!");
CharMove.enabled = false;
CharShoot.enabled = false;
this.enabled = false;
playerCol.enabled = false;
}
}
(Enemy's script)
using UnityEngine;
using System.Collections;
public class MeleEnemyAi : MonoBehaviour {
public float fpsTargetDistance;
public float enemyLookDistance;
public float attackDistance;
public float enemyMovementSpeed;
public float enemyAttackSpeed;
public float damping;
public float enemyDamage;
public GameObject fpsTarget;
public GameObject centreOfScene;
public CharacterSetup CharSetup;
public bool isAttackingPlayer;
Rigidbody rigidbody;
Renderer myRenderer;
// Use this for initialization
void Start () {
myRenderer = GetComponent<Renderer> ();
rigidbody = GetComponent<Rigidbody> ();
fpsTarget = GameObject.FindWithTag ("Player");
centreOfScene = GameObject.Find ("centreOfScene");
CharSetup = fpsTarget.GetComponent<CharacterSetup> ();
isAttackingPlayer = false;
}
// Update is called once per frame
void Update(){
while (fpsTargetDistance <= 2) {
attackPlayer ();
}
}
void FixedUpdate () {
fpsTargetDistance = Vector3.Distance (fpsTarget.transform.position, transform.position);
if (fpsTargetDistance < enemyLookDistance) {
myRenderer.material.color = Color.yellow;
lookAtPlayer ();
//print ("The enemy can see you");
} else {
walkToCentre ();
}
if (fpsTargetDistance < attackDistance) {
myRenderer.material.color = Color.red;
RunToPlayer ();
//print ("The enemy is attacking you");
} else {
myRenderer.material.color = Color.blue;
}
}
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Player") {
attackPlayer ();
}
}
void lookAtPlayer (){
Quaternion rotation = Quaternion.LookRotation(fpsTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
void RunToPlayer (){
rigidbody.AddForce (transform.forward * enemyAttackSpeed);
}
void walkToCentre(){
Quaternion rot = Quaternion.LookRotation (centreOfScene.transform.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime * damping);
rigidbody.AddForce (transform.forward * enemyMovementSpeed);
}
void attackPlayer(){
CharSetup.isBeingAttacked = true;
CharSetup.amountOfDamage = enemyDamage;
isAttackingPlayer = true;
}
}
As you can see i tried to store the enemies in an array so that the player's script could could check the isAttackingPlayer bool on the enemy's script but i couldn't figure out how to reference the isAttackingPlayer bool. I'm new enough to unity so maybe i'm just doing something really stupid, please help me.
Answer by EDevJogos · Jul 29, 2016 at 01:24 PM
In your character setup you have the following
while (isBeingAttacked == true) {
Deducthealth ();
}
Once isBeingAttacked becames true and enter this while, it never gets out, causing an infinity loop, and freezing unity.
Also im pretty sure you don't need this while, since this is inside a Update call you can just check if isBeingAttacked is true, and it will check it for you every frame.
if(isBeingAttacked)
{
Deducthealth ();
}
Also
while (fpsTargetDistance <= 2) {
attackPlayer ();
Once that condition is met it will never end, because this method will never exit in order for Unity to move on to the next frame
That was the problem alright. It worked fine when i used an else statement to turn of isAttackingPlayer. Thanks so much for the answer i was stuck on that for a while.
Your answer
![](https://koobas.hobune.stream/wayback/20220612085717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Don't get my app running on device IOS5, Xcode 4.2 2 Answers
Are downloads of Unity pro protected against crashes or other issues while downloading? 1 Answer
Unity 5.2.1 crashes importing 3rd party assets. 0 Answers
Unity Crashes when baking lighting. 1 Answer
My Unity Editor crashes when i try to enter playmode 1 Answer