I am attempting to make an extremely simplistic turn based strategy game that is currently using the onTrigger enter function to determine if two units are attacking one another. Each unit is a plane that has both a mesh collider that is a trigger and a rigidbody. The code in question is below and is the main control method for moving units across the board. I do not know if something is interfering with onTriggerEnter or if I am just not using it correctly. Any help is appreciated.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class container : MonoBehaviour {
public int index;
public int player;
public GameObject unitSpawner;
private UnitGraphic arrayAccess;
public bool isSelected = false;
public int moves = 0;
public int i;
private int minX = 5;
private int maxX = 495;
private int maxY = -5;
private int minY = -495;
private Vector3 myPosition;
private Vector3 previousPosition;
public container enemy;
Collider test;
void Start (){
arrayAccess = unitSpawner.GetComponent<UnitGraphic> ();
if (player == 0) {
moves = arrayAccess.player1.uStorage [index].movement;
}
if (player == 1) {
// moves = arrayAccess.player2.uStorage [index].movement;
}
myPosition = gameObject.transform.position;
}
void Update () {
float xBind = Mathf.Clamp (transform.position.x, minX, maxX);
float yBind = Mathf.Clamp (transform.position.y, minY, maxY);
myPosition = new Vector3(xBind, yBind, transform.position.z);
previousPosition = transform.position;
Mathf.Clamp (myPosition.y, minY, maxY);
if (isSelected.Equals (true)) {
moving();
}
}
void moving() {
if (i <= moves) {
if (Input.GetKeyUp (KeyCode.W)) {
transform.Translate (Vector3.forward * 601 * Time.deltaTime);
i++;
}
if (Input.GetKeyUp (KeyCode.D)) {
transform.Translate (Vector3.right * 601 * Time.deltaTime);
i++;
}
if (Input.GetKeyUp (KeyCode.S)) {
transform.Translate (Vector3.back * 601 * Time.deltaTime);
i++;
}
if (Input.GetKeyUp (KeyCode.A)) {
transform.Translate (Vector3.left * 601 * Time.deltaTime);
i++;
}
}
}
public void onTriggerEnter (Collider col) {
Debug.Log (col.gameObject.name);
if (col.gameObject.name != "Tile Map") {
enemy = col.gameObject.GetComponent <container> ();
if (enemy != null) {
if (enemy.player != player) {
if (enemy.player == 0) {
arrayAccess.player1.uStorage [enemy.index].health -= arrayAccess.player2.uStorage [index].attack;
enemy = null;
}
if (enemy.player == 1) {
arrayAccess.player2.uStorage [enemy.index].health -= arrayAccess.player1.uStorage [index].attack;
enemy = null;
}
}
if (enemy.player == player) {
transform.position = previousPosition;
enemy = null;
}
}
}
}
}