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 /
  • Help Room /
avatar image
0
Question by Dragon-Of-War · Sep 04, 2016 at 08:45 PM · editorcrash

Unity crashes when i get to fight the boss

So have this boss and when i get to fight him, after some seconds the editor crashes.

Here's the log.

The Boss's script:

 using UnityEngine;
 using System.Collections;
 
 public class EntScript : EnemyScripts {
     
     //===========//
     //[VARIABLES]//
     //===========//
 
     public enum Actions{Attack,Walk,Nothing};    // All of the Ent's actions
 
     bool doAction = true;                        // Can the ent do an action?
     Actions currentAction = Actions.Nothing;    // Action the the enemy is preforming
     Vector3 desiredPosition;                    // Position that the enemy will move
 
     //===========//
     //[FUNCTIONS]//
     //===========//
 
     //Attack action
     //This will make the ent shoot in the player's direction
     void Attack(){
         //Plays animation
         GetComponent<Animator> ().SetTrigger ("Attack");
 
         //Look at the player
         Vector3 pos = player.position - transform.position;
         pos.y = 0;
         Quaternion rot = Quaternion.LookRotation (pos);
 
         while (transform.rotation != rot) {
             transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime * 5f);
         }
 
         //Shoot
         Shoot (6,0);
 
         //The enemy is not preforming action
         currentAction = Actions.Nothing;
     }
 
     //Walk action
     //Will make the ent walk to a random position
     void Walk(){
         //Play animation
         GetComponent<Animator> ().SetBool ("Walking",true);
 
         //Chooses a random direction
         if (desiredPosition == Vector3.zero) desiredPosition = ChooseRandomPosition ();
 
         //Walks at the chosen direction
         bool gotToPosition = WalkTo(desiredPosition);
         if (gotToPosition) {
             desiredPosition = Vector3.zero;
             currentAction = Actions.Nothing;
             doAction = true;
         }
     }
 
     //Return a random position
     Vector3 ChooseRandomPosition(){
         Vector3 roomPos = room.transform.position;
         Vector3 pos = new Vector3 (roomPos.x + Random.Range(-4.0f,4.0f),transform.position.y,roomPos.z + Random.Range(-4.0f,4.0f));
         return pos;
     }
 
     //Timer
     IEnumerator Timer(){
         yield return new WaitForSeconds (1f);
         doAction = true;
     }
 
     //Returns a random Action
     Actions GetRandomAction(){
         Actions action;
         action = Random.value > 0.5f ? Actions.Attack : Actions.Walk;
         return action;
     }
 
 
     void FixedUpdate () {
         //If the enemy isn't preforming any actions, choose a action for him.
         if(currentAction == Actions.Nothing&&doAction){
             doAction = false;
             currentAction = GetRandomAction ();
         }
 
         //If the enemy is preforming a action and...
         switch (currentAction){
             //...this action is attacking
             case Actions.Attack:
                 //print ("Attacking");
                 Attack ();
                 StartCoroutine (Timer ());
                 break;
             //...this action is walking
             case Actions.Walk:
                 //print ("Walking");
                 Walk ();
                 break;
 
         }
     }
 }
 

The EnemyScript (the parent of the boss's script)

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScripts : MonoBehaviour {
 
     //===========//
     //[VARIABLES]//
     //===========//
 
     //Public variables
     public int health;                    //Enemy health
     public float speed;                    //Enemy speed
     public int damage;                    //Damage to deal
     public Transform player;            //Player
     public GameObject mesh;                //Mesh to apply damage effet
     public Material damageMat;            //Damage effect material
     public ParticleSystem particles;    //Damage Particles
     public Room room;                    //Room that the enemy is in
     public Transform[] shootPos;        //Shoot positions
     public GameObject[] bullets;        //Bullets that the enemy shoot
     public float bulletSpeed;            //How fast the bullet travel
     public float range;                    //How long the bullet will travel
 
     //Private variables
     //NavMeshAgent navAgent;            S//Mesh agent
 
     //===================//
     //[PRIVATE FUNCTIONS]//
     //===================//
 
     // Apply values
     void Awake(){
         player = GameObject.FindGameObjectWithTag ("Player").transform;
         //navAgent = GetComponent<NavMeshAgent> ();
     }
 
     //Adds red tint to the enemy when he's damaged and instantiates particles
     IEnumerator DamageEffect(){
         //Add material effect
         Material originalMat = mesh.GetComponent<Renderer> ().material;
         mesh.GetComponent<Renderer> ().material = damageMat;
 
         //Add particle effect, if there is one
         if (particles){
             ParticleSystem newParticles = Instantiate (particles,transform.position,transform.rotation) as ParticleSystem;
             Destroy (newParticles.gameObject,1f);
         }
 
         //Change back to the original material
         yield return new WaitForSeconds (0.1f);
         mesh.GetComponent<Renderer> ().material = originalMat;
     }
 
     //Receive Damage
     void ReceiveDamage(int receivedDamage){
         //Take damage
         if (health - receivedDamage >= 0)
             health -= receivedDamage;
         else
             health = 0;
         
         //Add a damage effect
         StartCoroutine (DamageEffect ());
 
         //Check if the enemy died
         if (health == 0) {
             if (room)
                 room.EnemyDied ();
             Destroy (gameObject);
         }
     }
 
     //Deal damage to player
     void OnTriggerStay(Collider other){
         if (other.tag == "Player") {
 
             //Add knockback
             Vector3 dir = other.transform.position - transform.position;
             dir.y = 0;
 
             //Deal damage and apply knockback
             other.gameObject.GetComponent<PlayerScript> ().ReceiveDamage(damage,dir,50f);
         }
     }
 
     //==================//
     //[PUBLIC FUNCTIONS]//
     //==================//
 
     //Shoot projectiles
     public void Shoot(int activeShootPos,int activeBullets){
         //Bullets that the enemy will shoot
         GameObject bullet = bullets [activeBullets];
 
         for (int i = 0; i < activeShootPos; i++) {
             //Where the bullets will come from
             Transform SPos = shootPos [i];
 
             //Creates bullets//
 
             //Instantiate bullet
             GameObject newBullet = (GameObject)Instantiate (bullet,SPos.position,SPos.rotation);
             //Sets the bullet's values
             newBullet.GetComponent<BulletScript> ().bulletSpeed = bulletSpeed;
             newBullet.GetComponent<BulletScript> ().damage = damage;
             //Destroy's the bullet
             Destroy (newBullet,range);
         }
     }
         
     //Walk to a given position
     public bool WalkTo(Vector3 targetPosition){
         //Look at the desired position
         Vector3 pos = targetPosition - transform.position;
         pos.y = 0;
         Quaternion rot = Quaternion.LookRotation (pos);
         transform.rotation = Quaternion.Slerp (transform.rotation,rot,Time.deltaTime*5f);
 
         //Walk to the position
         GetComponent<Rigidbody> ().AddForce (transform.forward*speed);
 
         print ((transform.position - targetPosition).magnitude);
 
         //If the enemy is close to the target position, return true.
         //If not, return false.
         if ((transform.position - targetPosition).magnitude > 2)
             return false;
         else
             return true;
     }
 
     //Attack the player
     public void AttackPlayer(){
         //Looks at the player
         Vector3 pos = player.position - transform.position;
         pos.y = 0;
         Quaternion rot = Quaternion.LookRotation (pos);
         transform.rotation = Quaternion.Slerp (transform.rotation,rot,Time.deltaTime*5f);
 
         //Move's towards the player
         GetComponent<Rigidbody> ().AddForce (transform.forward*speed);
     }
 }
 

editor.txt (144.0 kB)
Comment
Add comment · Show 1
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 Cherno · Sep 04, 2016 at 09:04 PM 0
Share

Have you tried inserting Debug.Log lines to see which party of your code are reached and which might cause the crash?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dragon-Of-War · Sep 04, 2016 at 09:43 PM

Line 30. It's a while loop.

Comment
Add comment · 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

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

61 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editor crashes one out of around three times with multiple editors open 0 Answers

Unity Editor CRashes Instantly after load (pls helps D:) 0 Answers

My pathfinding code crashes the editor 1 Answer

Crash when reimporting custom assets 0 Answers

Unity compilation error even when VS compiles fine 0 Answers


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