Dear: Readers
I have this problem that has been bothering me for awhile now. Want to ask you all to see if you can help me solve it, with another perspective.
I am designing a TD game, and having problems with the que system. Right now I have it set up, as a enemy comes in the turret collider, the turret adds 1 to “counter” variable and when exits take 1 away from “counter”. When “counter <=0” then i have the “LookBackToOrg()” function kick in. When the “currentTarget == null” and “counter >= 1” search for a target. I use a SphereCollider to gather targets and choose one based on other variables the enemy is holding. eg health, speed, so on.
The problem lays in the “counter” with enter and exit. As when a enemy gets destroyed inside a collider the turret does not register it as a exit.
Hope someone can help out with the problem, maybe even come at it with a totally new perspective.
Thank You: James
You have multiple solutions. I will give you an easy one to understand even if it’s not the best one.
Add List<> or turrets in your enemies script. OnTriggerEnter, you add the turret to the enemy list.
Then, OnDestroy (in your enemy script) loop on your turrets to notify that the enemy has been killed
the other solution is to have something like
public Action<EnemyScript> OnBeKilled = null;
private void OnDestroy()
{
if(OnBeDestroy != null)
{
OnBeDestroy(this);
}
}
On your EnemyScript. (Off course, replace EnemyScript by your Enemy class).
And then, in your Turret:
private void OnTriggerEnter(Collider pCollider)
{
EnemyScript enemy = pCollider.GetComponent<EnemyScript>();
enemy.OnBeKilled += OnEnemyBeKilled;
//replace with the real name of your variable
enemyCounter++;
}
private void OnTriggerExit(Collider pCollider)
{
EnemyScript enemy = pCollider.GetComponent<EnemyScript>();
enemy.OnBeKilled -= OnEnemyBeKilled;
//replace with the real name of your variable
enemyCounter--;
}
private void OnEnemyBeKilled()
{
//replace with the real name of your variable
enemyCounter--;
}
You can solve this by using a semi-complicated method called Event Handlers. Essentially, you would create a simple Delegate and event handling system in your enemy’s base class (if it doesn’t have one, make one - these are especially useful!) and then have objects that are dependent on specific non-Unity events receive notifications when certain things happen.
public class UnitBase : MonoBehaviour
{
// Declare our delegate type. Functions that register
// with events of this type much also take a 'unit' argument.
public delegate void UnitDelegate(UnitBase unit);
// This event acts as a callback for objects that need
// to know when this unit dies and is destroyed.
public event UnitDelegate OnUnitDestroyed;
// OnDestroy is a standard MonoBehaviour function and gets
// called automatically when an object is freed from memory
// we will use this as our Invocation function.
public void OnDestroy()
{
if(OnUnitDestroyed != null) OnUnitDestroyed(this);
}
}
Then in your Turret code, you would simply register for the OnUnitDestroyed event in the code portion where you are performing counter + 1.
UnitBase unit = currentTarget.GetComponent<UnitBase>();
if(unit != null) {
// To prevent this from getting called multiple times,
// it is a good idea to Unregister immediately before
// registering when utilizing events outside of Awake.
unit.OnUnitDestroyed -= HandleOnUnitDestroyed;
unit.OnUnitDestroyed += HandleOnUnitDestroyed;
}
And then create an event handler function that does this:
private void HandleOnUnitDestroyed(UnitBase unit)
{
// The unit was destroyed, so unregister!
if(unit != null) unit.OnUnitDestroyed -= HandleOnUnitDestroyed;
--counter;
}
Then in the place where you normally decrement your counter variable, call the handle function instead:
// Substitute the word "currentTarget" with whatever object or component you
// have access to in the function you're using for this.
HandleOnUnitDestroyed(currentTarget.GetComponent<UnitBase>());
This makes it so that any class derived from “UnitBase” has its destruction event unregistered when its supposed to as well as process your decrementer.