Hiya,
so I don’t know if it’s send message, or my receiving script but somewhere my numbers are occasionally being counted twice. I’ve looked through it many times, and i can only assume it’s based on the speed of my machine or that the messages are being sent but it’s happening and I don’t know how to stop it.
This is probably coded pretty much backwards but it’s working so I’m going with it for now…
I have a spaceship body with two wings, each wing has multiple guns, those guns need to be blown up individually before the ship will explode. On each gun I have this health script. When that gun gets destroyed it sends a message upwards that it’s gone, and a ticker counts down for that wing, if the ticker hits 0 the wing on that side gets the message to blow off. What I’m assuming is happening is the bullets are flying so fast the gun is registering it is at or under 0 HP multiple times and thus sending the message twice. which is being picked up twice and that gun is counted twice.
Gun Health Apply Damage Script
function ApplyDamage (damage : float)
{
health -=damage;
if (health <=0)
{
//un targetable by other weapons
gameObject.tag = null;
//turns off the barrel
Gun.SetActiveRecursively(false);
//explosion fx
Instantiate(SegmentedExplosion, transform.position, transform.rotation);
//creates smoke and links it to the wing, so when the wing floats off the smoke trails with it.
var Smoke : GameObject;
Smoke = Instantiate(SegmentedSmoke, transform.position+Vector3(0,0,-1), transform.rotation);
Smoke.transform.parent = Wing.transform;
//unparents the geo from the wing, parents it to the body and moves it off screen. This way all objects will disappear when the full ship blows up
transform.parent = Geo.transform;
transform.position=Vector3(0,100,0);
//sends the message that this gun is destroyed.
gameObject.SendMessageUpwards("PartDestroyed",side);
}
}
Section of the main ship that registers the “part destroyed” message.
function PartDestroyed(location:float){
parts--;
if(location==1)
{
Leftside--;
if(Leftside<=0){
BroadcastMessage("SIDE1");
}
}
if(location==2)
{
Rightside--;
if(Rightside<=0){
BroadcastMessage("SIDE2");
}
}
if (parts<=0){
BroadcastMessage("SIDE1");
BroadcastMessage("SIDE2");
BroadcastMessage("SIDE3");
yield WaitForSeconds(11);
Destroy(gameObject);
}
}
Everything works, just not fast enough. I know send message is slow, but I don’t know a way to direct cal weather specific guns have been destroyed or not. Any help would be awesome.