Check if with a spawned gameobject

I have this line of code that destroys a child of a prefab (works) and also I want to check for a certain spawned prefab and increase my gold by 20 if it dies (does not work).

 if (ai && !ai.dead){ // if ai.dead not set yet...
    ai.dead = true; // set it and destroy capsule
    Destroy(transform.Find("Capsule").gameObject);
		if (gameObject.name == "RightGrunt(Clone)"){gold += 20;}
    }

Apparently it doesn’t work because it is a spawned prefab. If I do this with the same prefab set in the scene it works. How can apply the if for the spawned prefab ?

Code is in C#

Anyone can shed some light on why this is happening ?

How is it possible that the code works for a prefab already in the game, but not for the same prefab that is spawned ? For days I;m thinking at this and I have no idea…