- Home /
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;
}
}
}
Put some debug on your TriggerExit and see if RandomPosition is moving unit out of trigger area and not firing event?
I see below Answers related to restructuring GetComponent - but GetComponent does not "...work 50% of the time".
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.
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!
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
Your answer
