Hi guys:
I have a strange problem in my recent project. It’s a 2D first person shoot game. I generate the enemies stochastically out of the screen,(because I don’t want the enemy shows up abruptly in the screen).
The character has a shoot range, but it’s bigger than the screen, this means the character can shoot the enemies out of the screen. In most cases it works fine. But sometimes, in editor mode test while the character is shooting an enemy out of screen, there will be a null reference exception pop out.
Here is the code below:
function ApplyDamage(damage : float){
health -= damage;
cannotBeSeenTime = criticalTime;
if(health <= 0){ // killed
SendMessage(“OnDeath”, SendMessageOptions.DontRequireReceiver);
return;
}
if(canBeSeen highlightMap != null){
// This sentence will throw exception
skinMaterial.SetTexture(“_BumpMap”, highlightMap);
highlightingTime = highlightTime;
}
SendMessage(“GotHit”, SendMessageOptions.DontRequireReceiver);
}
This function is used when the gun detected the enemy, and call this function of the enemy’s script. The exception is throwed in this sentence: skinMaterial.SetTexture(“_BumpMap”, highlightMap); And this phrase is used to make the enemy shining a little while when got shot. As I said it works fine in most cases.
Any suggest is fine. Thanks.