How do I disable enemy ai the script after enemy ai dies
I need to disable the script when the enemy ai die.Because what is going on is the enemy ai is follow the player after and before it dies .
Here is the enemy ai attack, idle , and move script :
using UnityEngine;
using System.Collections;
public class Enemyai : MonoBehaviour {
public Transform player;
static Animator anim;
void Start ()
{
anim = GetComponent<Animator> ();
}
public void Stop(){
anim.SetBool("isWalking", false);
anim.SetBool("isAttack", false);
anim.SetBool("isIdle", true);
this.enabled = false;
//Or if you want to destroy the AI script completely
//Destroy(this)
}
void Update ()
{
if (Vector3.Distance(player.position, this.transform.position) < 20)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
Player.gameObject.activeSelf;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2.6 )
{
this.transform.Translate(0,0,0.09f);
anim.SetBool("isMoving",true);
anim.SetBool("isAttack",false);
}
else
{
anim.SetBool("isAttack",true);
anim.SetBool("isMoving",false);
}
}
else
{
anim.SetBool("isIdle",true);
anim.SetBool("isMoving",false);
anim.SetBool("isAttack",false);
}
}
}
Enemy ai has to deactivate first and then the die animation play on the health script :
private void DieLocal(Vector3 position, Vector3 force)
{
// Notify those interested.
EventHandler.ExecuteEvent(m_GameObject, "OnDeath");
EventHandler.ExecuteEvent<Vector3, Vector3>(m_GameObject, "OnDeathDetails", force, position);
// Spawn any objects on death, such as an explosion if the object is an explosive barrell.
for (int i = 0; i < m_SpawnedObjectsOnDeath.Length; ++i) {
ObjectPool.Instantiate(m_SpawnedObjectsOnDeath[i], transform.position, transform.rotation);
}
// Destroy any objects on death. The objects will be placed back in the object pool if they were created within it otherwise the object will be destroyed.
for (int i = 0; i < m_DestroyedObjectsOnDeath.Length; ++i) {
if (ObjectPool.SpawnedWithPool(m_DestroyedObjectsOnDeath[i])) {
ObjectPool.Destroy(m_DestroyedObjectsOnDeath[i]);
} else {
Object.Destroy(m_DestroyedObjectsOnDeath[i]);
}
}
// Deactivate the object if requested.
if (m_DeactivateOnDeath) {
Scheduler.Schedule(m_DeactivateOnDeathDelay, Deactivate);
GetComponent<AudioSource>().Play();
anim.SetTrigger("isDead");
}
}
I am a little confused. Are there two different scripts here? As I am understanding it, you have a character running toward the player and then dies and plays some death animation, but keeps chasing the player while doing those animations? I think I just need a few more details to be sure on this one.
One simple solution would be to have your health script simply do something like this:
gameObject.GetComponent<Enemyai>().SetEnabled(false);
That should take care of your enemy continuing to move. All you have to do is make sure you don't call it too early or too late so all the right things happen.
Answer by iDerpCake · Jul 28, 2016 at 01:46 AM
A easy way to disable the script is to run this after all the scripts of the enemy ai has ran
GetComponent<"Name of Script">().enabled = false;
I got the enemy ai to stop following me in the scene with your code . I think it might be a bug. I had a ridgybody on the enemy ai and I delete that to see if the enemy will follow me and it wont. I had a sphere and capsule collider and I deleted capsule collider and still didn't follow. I think its a bug or going on with the scene. I figure out how I got the stop.
Your code work out thanks.I did it like this :
GetComponent().enabled = false;
It wasn't a bug.