I have a script attached to a GameObject which does the following:
- Determines a target GameObject and stores its ‘entity’ script in currTarget
- Subscribes to the onDeath event of currTarget
- Attacks currTarget
- Calls Destroy(gameObject) when dead.
Now, let’s say I have three GameObjects with the ‘entity’ script attached to them, A, B, and C. A is an enemy of both B and C. A’s current target is B, and both B and C target A.
A kills B, so B Destroy()s itself and fires its onDeath event (captured by A). Many frames later, C manages to kill A, so A fires its own onDeath event. What happens now is this event is captured by both C (still alive) and B (supposedly destroyed).
Strangely enough, this test if statement actually passes as true (and the Debug message is displayed):
if (this == null) Debug.Log("I should be dead!");
…so how can something that is null execute in the first place?
Am I missing something here? It seems very strange that scripts should still be running if their parent GameObject has been destroyed.