- Home /
Object Tower not selecting right object.
So i have a object that whenever another object that has the enemy tag enters its box collider its suppose to add that object to its array list. The object then sends the first element of the one that been in the collider the longest to the arrow script giving the arrow a target to shoot. Also whenever a enemy cube enters the collider it sends its transform info to the tower class so it can be added to the array. So whats suppose to happen is a enemy enters the area, and the tower shoots it. But whats happening is the tower is shooting the next target or a completely different target than the one that its suppose to be shooting and im not sure what the problem is.
Here is the TowerClass:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ArcheryTowerScript : MonoBehaviour {
public GameObject Arrow;
public Transform myTransform;
public Transform selectedTarget;
public Transform[] EnemyArray = new Transform[10];
public int value = 0;
public int value1 = 0;
public Transform Cube;
// Use this for initialization
IEnumerator Start () {
myTransform = selectedTarget;
for(int x = 0; x < 10; x++)
{
Instantiate(Arrow, new Vector3(myTransform.position.x,myTransform.position.y,myTransform.position.z), Quaternion.identity);
yield return new WaitForSeconds(3);
x = 0;
}
}
// Update is called once per frame
void Update () {
if(EnemyArray[0] == null)
{
for(int x = 0; x < EnemyArray.Length; x++)
{
EnemyArray[x] = EnemyArray[x+1];
}
}
}
void OnTriggerEnter(Collider collision)
{
if(collision.transform.tag == "Enemy")
{
EnemyArray[value] = Cube;
value++;
}
}
void OnTriggerExit(Collider collision)
{
if(collision.transform.tag == "Enemy")
{
value--;
EnemyArray[0] = null;
for(int x = 0; x < EnemyArray.Length; x++)
{
EnemyArray[x] = EnemyArray[x+1];
}
}
}
public Transform recentEnemy()
{
return EnemyArray[0];
}
public void setCube(Transform myCube)
{
Cube = myCube;
}
}
Heres the Arrow Class:
void Update () {
ArcheryTower = GameObject.Find("ArcheryTower");
ArcheryTowerScript at = new ArcheryTowerScript();
at = ArcheryTower.GetComponent<ArcheryTowerScript>();
selectedTarget1 = at.recentEnemy();
Vector3 dir = selectedTarget1.position - myGameObject.position;
myGameObject.rotation = Quaternion.Slerp(myGameObject.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
myGameObject.position += myGameObject.forward * moveSpeed * Time.deltaTime;
}
void OnTriggerEnter(Collider collision)
{
if((collision.gameObject.name == "MiniUnit(Clone)")||(collision.gameObject.name == "MediumUnit(Clone)")||(collision.gameObject.name =="LargeUnit(Clone)"))
{
Destroy(Arrow);
}
}
}
And Heres the cube class:
void OnTriggerEnter(Collider collision)
{
ArcheryTower = GameObject.Find("ArcheryTower");
ArcheryTowerScript at = new ArcheryTowerScript();
at = ArcheryTower.GetComponent<ArcheryTowerScript>();
at.setCube(Cube);
value = 1;
if(collision.gameObject.name == "UltraCube")
{
orientation = 1;
}
if(collision.gameObject.name == "Arrow(Clone)")
{
AdjustCurrentHealth(-100);
}
}
IEnumerator OnTriggerExit(Collider collision)
{
if(collision.gameObject.name == "UltraCube")
{
yield return new WaitForSeconds(1);
orientation = 0;
}
}
public void AdjustCurrentHealth(int adj)
{
health += adj;
if(health < 0)
{
health = 0;
}
if(health == 0)
{
Destroy(gameObject);
}
}
Answer by Deadcow_ · Oct 26, 2014 at 12:44 PM
Why you're using arrays for this? I think List will be much better. You can just simply .Add() new enemy, .Remove() enemy if it dead or comes to OnTriggerExit and get .First() and .Last() enemy whenever you want without addotional calculations and variables