Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OTG_01 · Jul 29, 2016 at 12:29 PM · crashesplayer damage

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

@OTG_01

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrpmorris · Jul 29, 2016 at 01:33 PM 1
Share

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

avatar image OTG_01 · Jul 29, 2016 at 06:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges