Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

MissingReferenceException: The object of type 'EnemyController' has been destroyed but you are still trying to access it.

$
0
0
I have an enemy that I've duplicated as I want more than one of the same type of enemy. However, once I kill one of the enemies and then take damage from the other enemy, the game crashes. It's like each cloned enemy is not unique. The script for controlling the duplicated enemy is destroyed, but I don't want it to be destroyed. Thank you EnemyController Class public class EnemyController : MonoBehaviour { public float lookRadius = 10f; Transform target; NavMeshAgent agent; // Use this for initialization void Start () { target = PlayerManager.instance.player.transform; agent = GetComponent(); } // Update is called once per frame void Update () { float distance = Vector3.Distance(target.position, transform.position); if(distance <= lookRadius) { agent.SetDestination(target.position); if(distance <= agent.stoppingDistance) { // Attack the target FaceTarget(); } } } void FaceTarget() { Vector3 direction = (target.position - transform.position).normalized; Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z)); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f); } public float returnEnemyX() { float location = transform.position.x; return location; } private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, lookRadius); } } PlayerCollision Class public class PlayerCollision : MonoBehaviour { public Health2 H2; public PlayerMovement movement; public Rigidbody rb; public EnemyController enemyController; public gems gems; public EnemyStats enemyStats; public int damageEnemyBy = 100; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } private void OnCollisionEnter(Collision collisionInfo) { if (collisionInfo.collider.tag == "spikeMoveUp") { Debug.Log("You hit the spike"); H2.setHealth(H2.getHealth() - 1); StartCoroutine(Flasher()); if (H2.getHealth() <= 0) { GetComponent().material.color = Color.red; movement.enabled = false; FindObjectOfType().EndGame(); } } //! Jump on enemy if (collisionInfo.collider.tag == "Enemy" && movement.onGround == true && transform.position.y >= 2) //! CHANGED movement.onGround == true (instead of false). { Debug.Log("Enemy has taken damage"); //collisionInfo.gameObject.GetComponent().Hit(10); enemyStats.Hit(damageEnemyBy); if(enemyStats.getHealth() == 0) { //if (gameObject != null) Destroy(collisionInfo.gameObject); } //Destroy(collisionInfo.gameObject); } //! Enemy attack you - d else if (collisionInfo.collider.tag == "Enemy") //(collisionInfo.collider.tag == "Enemy" && Input.GetKey("d")) { if (transform.position.x < enemyController.returnEnemyX()) { //H2.setHealth(H2.getHealth() - 1); Debug.Log("The enemy hit you"); H2.setHealth(H2.getHealth() - 1); StartCoroutine(Flasher()); rb.AddForce(-500 * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (transform.position.x > enemyController.returnEnemyX()) { Debug.Log("The enemy hit you"); H2.setHealth(H2.getHealth() - 1); StartCoroutine(Flasher()); rb.AddForce(500 * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (H2.getHealth() <= 0) { GetComponent().material.color = Color.red; movement.enabled = false; FindObjectOfType().EndGame(); } } //else if (collisionInfo.collider.tag == "Enemy") //{ //} //! Collect gem if(collisionInfo.collider.tag == "Gem") { Debug.Log("You got a gem!"); Destroy(collisionInfo.gameObject); //H2.setHealth(H2.getHealth() + 1); gems.setGems(gems.getGems() + 1); Debug.Log(gems.getGems()); } } IEnumerator Flasher() { //GetComponent().material.color = Color.red; //yield return new WaitForSeconds(.5f); //GetComponent().material.color = Color.black; if (H2.health >= 1) { for (int i = 0; i < 1; i++) { GetComponent().material.color = Color.red; yield return new WaitForSeconds(.1f); GetComponent().material.color = Color.black; yield return new WaitForSeconds(.1f); } } } } PlayerManger Class public class PlayerManager : MonoBehaviour { #region Singleton public static PlayerManager instance; private void Awake() { instance = this; } #endregion public GameObject player; }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>