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 importguru88 · Jul 28, 2016 at 12:34 AM · script.disableenemy aidie

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");
                         
                         }
         
 
 
         
         }
 


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 TobiasAndrion · Jul 28, 2016 at 02:02 AM 0
Share

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.

1 Reply

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

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;

Comment
Add comment · Show 2 · 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
avatar image importguru88 · Jul 28, 2016 at 05:22 AM 0
Share

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.

avatar image importguru88 · Jul 28, 2016 at 05:58 AM 0
Share

Your code work out thanks.I did it like this :

GetComponent().enabled = false;

It wasn't a bug.

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

54 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

Related Questions

Identical Lines of Code Will Either Disable, or Wont Disable a Script. Need Help. 0 Answers

What is wrong with my script? 1 Answer

Randomly play multiple attack animation 0 Answers

Disable an added script from First Person Character 0 Answers

Disable a script for a few seconds 2 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