Answers for "Null reference and GetComponent"http://answers.unity.com/questions/733814/null-reference-and-getcomponent.html
The latest answers for the question "Null reference and GetComponent"Answer by robertbuhttp://answers.unity.com/answers/733815/view.html
Your code is trying to get the Health script attached to the same game object as ExploGren. But according to your question, the Health script is attached to all the enemies. To fix:
- Delete line 23
- Insert between line 39 and 40:
----
damageByGrenadeExplosion = col.GetComponent<Health>();
if (damageByGrenadeExplosion != null)
A couple of other notes.
- damageByGrenadeExplosion can be declared inside the Update() function rather than at the class level.
- I don't know your game mechanic, but line 25 (where you call OverlapSphere) should probably be moved to between line 35 and 36 so that it has fresh data at the time of the explosion.Tue, 24 Jun 2014 04:16:28 GMTrobertbu