game freeze problem

as said in the title, the game freezes, but only on the built game… after trying again and again to find where it’s freezing, i see it’s when two of the same type of turret shoot two of the same type of zombie, each type of zombie has a different script, the turret sends a message to the scripts.

here is a section from the turret script:

function Fire () {
	if (target != null) {
	var dist : float = Vector3.Distance(target.position, turret.position);
	if(dist <= distance){
		target.SendMessage("CheapGun", gameObject);
	}
	}
}

and here from one of the zombies:

function CheapGun () {
	health -= CheapGunDam;
	var spawning = Instantiate(blood ,transform.position,Quaternion.identity);
}

function Death () {
	var death = Instantiate(ragdoll ,transform.position,Quaternion.identity);
	Destroy(gameObject);
	spawner.alive -= 1;
	Money.money += 10;
}

some help would be really appreciated as i’ve finished the game and can’t sell it with it freezing! if anyone knows a reason or something to try it’d be really appreciated!!! thanks

I don’t know if it’s what causing the freeze, but there’s something wrong with you code. Take a look at the documentation and you will see that the second parameter of SetMessage is SendMessageOptions, but you’re sending you’re game object. This parameter are optional, so your options are:

1 - target.SendMessage("CheapGun", SendMessageOptions.RequireReceiver);
2 - target.SendMessage("CheapGun", SendMessageOptions.DontRequireReceiver);
3 - target.SendMessage("CheapGun"); // Same as 1, cause RequireReceiver is the default value.

Also, in your Dead function you’re changing the values of your game object AFTER destroying it. Try to invert it.

function Death () {
    var death = Instantiate(ragdoll ,transform.position,Quaternion.identity);
    spawner.alive -= 1;
    Money.money += 10;
    Destroy(gameObject);
}

i don’t know what it was… but somehow i’ve fixed it! i went through removing NullRefrenceExceptions and other console errors kept trying it still wouldn’t work but continued and just tried it… it worked! thank you all for your input!