An odd bug in unity iphone

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.

  1. Where do you assign a valid reference to skinMaterial?
  2. I guess you know that but unless you target 3GS, there are no bumpmaps on the iDevices at all and without having pixel lights they also won’t work (and I don’t know if unity supports them as even the 3GS is a magnitude too slow for such stuff …)

private var skinRender : SkinnedMeshRenderer;
private var skinMaterial : Material;

function Start(){
skinRender = GetComponentInChildren(SkinnedMeshRenderer);
skinMaterial = skinRender.material;
}

this is how I get skinMaterial. And I use “Self-Illumin/Diffuse” shader for the material.

Actually I don’t think this is the problem, because this works fine for most cases. Only if the character keeps shooting the enemies out of the screen for a while, and about tens of enemies have been shot, this will happen once.

private var skinRender : SkinnedMeshRenderer;
private var skinMaterial : Material;

function Start(){
skinRender = GetComponentInChildren(SkinnedMeshRenderer);
skinMaterial = skinRender.material;
}

this is how I get skinMaterial. And I use “Self-Illumin/Diffuse” shader for the material.

Actually I don’t think this is the problem, because this works fine for most cases. Only if the character keeps shooting the enemies out of the screen for a while, and about tens of enemies have been shot, this will happen once.