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 /
avatar image
0
Question by JayFitz91 · Aug 23, 2015 at 10:15 AM · gameobjectontriggerexitenabled

Re-enabling a script OnTriggerExit

I have an enemy in my scene that when colliding with a slime gameobject on the ground, will have his script disabled, this works fine.

When the slime disappears though, I would like to re-enable his script so he can continue doing his thing. This only works 50% of the time though, the other times, the script will not re-enable and he continues to be stuck.

Anyone know what I'm overlooking here? Thanks

 using UnityEngine;
 using System.Collections;
 
 public class SwarmBehaviour : MonoBehaviour
 {
     private Animator animate;
     private NavMeshAgent agent;
 
     public float distance;
 
     private Transform target; //the enemy's target 
 
     float moveSpeed = 3; //move speed 
     float rotationSpeed = 3; //speed of turning
 
     private Transform myTransform; //current transform data of this enemy
     public Vector3 finalPosition;
 
     private Rigidbody rb;
 
     void Awake()
     {
         myTransform = transform; //cache transform data for easy access/preformance 
     }
 
     // Use this for initialization
     void Start()
     {
         target = GameObject.FindWithTag("Caecus").transform; //target the player
         animate = GetComponent<Animator>();
         agent = GetComponent<NavMeshAgent>();
         InvokeRepeating("RandomPosition", 0.0f, 5.0f);
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         distance = Vector3.Distance(myTransform.position, target.transform.position);
 
         if (distance < 10)
         {
             agent.enabled = false;
             
             animate.SetBool("Moving", true);
 
             //rotate to look at the player 
             myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
             //move towards the player
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
             rb.isKinematic = false;
 
         }
         else
         {
             rb.isKinematic = true;
             agent.enabled = true;
             agent.destination = finalPosition;
             //PathRemaining();
         }
     }
 
     void RandomPosition()
     {
         Vector3 randomDirection = Random.insideUnitSphere * 10.0f;
 
         randomDirection += transform.position;
         finalPosition = randomDirection;
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Slime")
         {
             animate.SetBool("Moving", false);
             GetComponent<SwarmBehaviour>().enabled = false;
         }
     }
 
     void OnTriggerExit(Collider col)
     {
         if (col.gameObject.tag == "Slime")
         {
             GetComponent<SwarmBehaviour>().enabled = true;
         }
     }
 
 }

Comment
Add comment · Show 3
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 getyour411 · Aug 23, 2015 at 10:25 AM 0
Share

Put some debug on your TriggerExit and see if RandomPosition is moving unit out of trigger area and not firing event?

avatar image getyour411 · Aug 23, 2015 at 06:04 PM 0
Share

I see below Answers related to restructuring GetComponent - but GetComponent does not "...work 50% of the time".

avatar image JayFitz91 · Aug 23, 2015 at 06:49 PM 0
Share

Sorry, been in work all day. Yes unfortunately, below answers do not address the issue, could you elaborate on what I should be doing with the Debugs? I have put them throughout the code (OnTriggerEnter, OnTriggerExit) but it appears as if everything should work correctly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pflobus · Aug 23, 2015 at 01:25 PM

I tested this out, and it works:

  (GameObject.Find("GameObject").GetComponent("ScriptName") as MonoBehaviour).enabled = true;

So OnTriggerExit you can just put this in, and it'll work!

Comment
Add comment · 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
0

Answer by alexander11 · Aug 23, 2015 at 05:59 PM

 void OnTriggerEnter(Collider col)
      {
          if (col.gameObject.tag == "Slime")
          {
              animate.SetBool("Moving", false);
              GetComponent<SwarmBehaviour>().enabled = false;
          }
      }
      Else
      {
                GetComponent<SwarmBehaviour>().enabled = true;
      }


Sorry haven't code in a couple of months but this is just from the top of my head see if this code helps

Comment
Add comment · 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

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

27 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

Related Questions

OnTriggerExit overwrites OnTriggerEnter 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Enable & Disable multiple game objects 0 Answers

If i disable a GameObject does all it's components get disabled also? 2 Answers

Enabling/Disabling Multiple Tagged objects 1 Answer


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