Hello folks,
to make it short: I have a prefab for an archer. This archer shoots a projectile at an enemy if the enemy is in the trigger of the current archer. It works if I drag the prefab of the archer to the scene window.
It still works for the previous archer prefab that it shoots the enemy OnTriggerEnter, but if I drag another archer (with the same prefab) to the scene view and the enemy enters the trigger of it, something seems to be wrong.
To make it clear, the projectile gets instantiated when a enemy (with a specific tag) is in the trigger of the current archer. It ‘flies’ to the target (enemy in trigger), using
gameObject.transform.LookAt (closestTarget.transform);
gameObject.transform.Translate (Vector3.forward * velocity * Time.deltaTime);
and destroys itself after entering the trigger of the enemy.
If the enemy enters the trigger of the second archer, the projectile gets instantiated but it never shoots, it just appears and gets instantly destroyed on the transform.postion of the archer.
What could be wrong, that it only works on the first archer (using the prefab), but not on the second archer (using the same prefab a second time)?
Greetings. 
First thing you should do is put some Debug.Log’s in you ontriggerenter code and see if it’s being called at all.
Already did that and it gets called. It even knows which enemy it is, but for some reason it doesn’t shoot like the other archer.
If it’s being called, then there is nothing wrong with the colliders. The issue must be with the code you are running within them.
This is (the part of) the script of the archer. Attack() gets called every time when an enemy is in the trigger of the archer.
void Attack ()
{
GameObject projectile;
projectile = Instantiate(attackGameObject, transform.position, Quaternion.identity);
projectileScript.closestTarget = closestTarget;
}
void OnTriggerStay(Collider unit)
{
if (unit.gameObject.tag == "UnitGround")
{
isInTrigger = true;
print (unit.gameObject.name + " is in trigger.");
}
}
void OnTriggerExit(Collider unit)
{
if (unit.gameObject.tag == "UnitGround")
{
isInTrigger = false;
}
That’s the projectile script:
// Use this for initialization
void Start ()
{
closestTarget = towerScript.closestTarget;
propertiesScript = closestTarget.GetComponent<Properties>();
}
void Update()
{
if (closestTarget != null)
{
gameObject.transform.LookAt (closestTarget.transform);
gameObject.transform.Translate (Vector3.forward * velocity * Time.deltaTime);
}
else
{
Destroy (gameObject);
}
}
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "UnitGround")
{
Invoke("Damage", 0.5f);
Destroy (gameObject, 1.0f);
}
}
void Damage()
{
propertiesScript.TakeDamage (amountOfDamage);
}
I would say this is probably your problem
projectileScript.closestTarget = closestTarget;
I would have the target set in each instance of the projectile rather than a public or static variable elsewhere. That way each projectile has it own target and you could have two projectiles at the same time with different targets.
Seems that my reply did not send. It works now, the mistake was the mentioned part of you.
Thank you!