I have created a script where if my enemy object collides in a certain area, it gets destroyed. Strangely, however, it is not working at all.
Here’s what you need to know:
- The “certain area” we are dealing has a sphere collider that has “is trigger” checked.
- The tag is labeled Light in the inspector
- The mesh renderer for the sphere is off (I still had problems when it was on)
- The enemy has an active box collider that also has “is trigger” checked.
- My script is as follows:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EnemyAI : MonoBehaviour {
public float speed;
public GameObject container;
public Respawn Container;
public PlayerContainer PlayerContainerScript;
void Awake(){
container = GameObject.FindGameObjectWithTag ("Container");
Container = container.GetComponent<Respawn> ();
PlayerContainerScript = container.GetComponent<PlayerContainer> ();
}
// Update is called once per frame
void Update () {
//Follow and Look at Player
float step = speed * Time.deltaTime;
Vector3 newPos = Vector3.MoveTowards (transform.position, PlayerContainerScript.player.position, step);
newPos.y = 0.5f;
transform.LookAt (newPos);
transform.position = newPos;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Light")) {
Debug.Log("Collide");
Destroy (gameObject);
Container.noCount = Container.noCount - 1;
}
}
}