To disable, you can only delete?

using UnityEngine; using System.Collections; public class Visor : MonoBehaviour { public string tagWall = "Wall"; public string tagTarget = "Enemy"; public GameObject agent; // Use this for initialization void Start() { if (agent == null) agent = gameObject; print("indexer"); } public void OnTriggerStay(Collider coll) { string tag = coll.gameObject.tag; if (!tag.Equals(tagTarget)) return; GameObject target = coll.gameObject; Vector3 agentPos = agent.transform.position; Vector3 targetPos = target.transform.position; Vector3 direction = targetPos - agentPos; float length = direction.magnitude; direction.Normalize(); Ray ray = new Ray(agentPos, direction); RaycastHit[] hits; hits = Physics.RaycastAll(ray, length); int i; for (i = 0; i < hits.Length; i++) { GameObject hitObj; hitObj = hits[i].collider.gameObject; tag = hitObj.tag; if (tag.Equals(tagWall)) return; } // TODO // target is visible code your behaviour below /* //вариант поведения с Arrive/Leave: GetComponent<Face>().enabled = true; GetComponent<Arrive>().enabled = true; GetComponent<Leave>().enabled = true; GetComponent<Wander>().enabled = false; */ //вариант поведения с pursue: GetComponent<Face>().enabled = true; GetComponent<Pursue>().enabled = true; GetComponent<Wander>().enabled = false; } public void OnTriggerExit(Collider coll) { /* //вариант поведения с Arrive/Leave: GetComponent<Face>().enabled = false; GetComponent<Arrive>().enabled = false; GetComponent<Leave>().enabled = false; GetComponent<Wander>().enabled = true; */ //вариант поведения с pursue: //if (coll.name == "Player") //{ GetComponent<Face>().enabled = false; GetComponent<Pursue>().enabled = false; GetComponent<Wander>().enabled = true; //} } } 
  • one
    Add code that clearly demonstrates the problem and, for example, a hierarchy screen. and describe what exactly is happening, why you decided that it works - Alexey Shimansky

1 answer 1

Turning off the script stops only the "periodic" methods - Update , LateUpdate , FixedUpdate . But the events (those that start with "On ..."), the collisions caused by the system still remain active.

Usually and most often for writing logic to an object, when it is written in a trigger like this:

  • Get the boolean flag.
  • On OnTriggerEnter make it true , on OnTriggerExit - false .
  • While the flag is true - to do actions. Or vice versa, if the flag is false , do nothing else ( if (!inTrigger) return; )

Example:

 bool inTrigger = false; void OnTriggerEnter(Collider other){ inTrigger = true; // other code } void OnTriggerExit(Collider other){ inTrigger = false; // other code } void Update(){ if (inTrigger){ // OnTriggerStay code MySuperMethodWhileStayInTrigger(); } // other code } void MySuperMethodWhileStayInTrigger() { // ... } 

The only thing that can happen is that several objects can fly into the trigger simultaneously, and then OnTriggerExit will work only on the first one that has flown out. Therefore, sometimes they do it this way:

 int inTrigger = 0; void OnTriggerEnter(Collider other){ inTrigger++; // other code } void OnTriggerExit(Collider other){ inTrigger--; // other code } void Update(){ if (inTrigger > 0){ // OnTriggerStay code MySuperMethodWhileStayInTrigger(); } } void MySuperMethodWhileStayInTrigger() { // ... } // other code 

That is, they consider "treggnuty". And if it is not 0, then someone is inside.