- Home /
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
is it freezing in the editor or just on the target? What target is it freezing on?
Try switching on the debug console in Unity, that might help some.
Also, I'm not too sure how exactly your code is structured, but destryoing the current object from inside is ALWAYS a bad idea, even if that's not the cause of your freezing.
it's freezing in the built game never actually in the unity editor test - it seems it shoots when two off the same zombies are being shot by two of the same turret - two targets are being sent the same message to identical scripts from two identical scripts.
the console is on... i should have mentioned, i'm getting nullRefrenceExceptions, a missingRefrenceException to the shoot effect - as it goes un-active, and an unassignedRefrenceException and PPtr a cast not dereferencing. not sure, will these be reasons of freezing?
missingRefrenceException means you're accessing an object that is not there anymore. If two turrets are killing the same zombie at same time, when the second shoots arrive the zombie object is destroyed already.
there are two clone turret prefabs, and two clones of the same zombie prefab... will a missingRefrenceException be a reason for freezing?
Answer by hvilela · Sep 06, 2012 at 04:14 PM
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);
}
Actually, Send$$anonymous$$essage has an overload that do take an object as parameter - and the receiving function can ignore it, so that's totally valid. As for the Destroy(), I've read somewhere that it will always yield until the method exit...or the frame ends, or something like that - I've never tested it, really - but I'm pretty sure that has nothing to do here. We don't have much of the Zombie script, I'm curious as to how Death() is called, etc...
i'm currently working to putting these suggestions into all my scripts... will keep you updated but thanks!
the death function is called in the update function
if(health <= 0){ Death(); }
i've changed all the parts you suggested hvilela but the game still freezes... :/
Answer by oinkoinkflapflap · Sep 08, 2012 at 11:37 AM
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!