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
Have you tried inserting Debug.Log lines to see which party of your code are reached and which might cause the crash?
Your answer

Follow this Question
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