void Start() { DeathSystem targetDeath = target.gameObject.GetComponent <DeathSystem>(); if (targetDeath != null) { targetDeath.RegisterDeathCallback(TargetDeath); } }
private void InitializeEnemyComponents(GameObject enemy) { Transform spawn = GetRandomSpawner(); if (spawn != null) { NavMeshAgent agent = enemy.GetComponent <NavMeshAgent>(); // disable the nav mesh agent to prevent a bug with the enemy spawning in the wrong location if (agent != null) { agent.enabled = false; } // set the spawn point enemy.transform.position = spawn.position; // move the enemy in a radius around the spawn point Vector2 radius = Random.insideUnitCircle * 10.0f; enemy.transform.Translate(radius.x, 0.0f, radius.y); // if the enemy uses a MoveTowardsTarget script, the target needs to be set ITargetBasedMovement moveTowards = enemy.GetComponent(typeof(ITargetBasedMovement)) as ITargetBasedMovement; if (moveTowards != null) { moveTowards.target = GameObject.FindGameObjectWithTag("Player").transform; } // register for death notification DeathSystem enemyDeath = enemy.GetComponent <DeathSystem>(); if (enemyDeath != null) { enemyDeath.RegisterDeathCallback(EnemyDeathCallback); } //increment live enemy count enemyCount++; // re-enable the nav mesh agent if (agent != null) { agent.enabled = true; } } }