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  _= MeleEnemies *.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 ();*
* rigidbody = GetComponent ();*
* fpsTarget = GameObject.FindWithTag (“Player”);*
* centreOfScene = GameObject.Find (“centreOfScene”);*
* CharSetup = fpsTarget.GetComponent ();*
* 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.

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 ();
}

@OTG_01