I have a script for my zombie enemies that detects which body parts have been destroyed, produces an instantiate of that body part, and keeps from checking again. It was tedious to set up and calls each part by name but it works smoothly and without any lag or real problems and successfully changes the animation states to crawling, falling, etc. The issue becomes that the spawn points for these parts have to move with the bones so they are inside of them, and when the part is destroyed so is the spawn, which is good. However if say you blast off the right hand, and the hand shoots out, and then later blast off the right elbow or whatever bone part is next up the line since the spawn or connected bones for that arm is no longer there it throws a nullreferenceexception. I want that to be a null reference, and I’m glad it comes back as such or the code wouldn’t work but I don’t need to see it in my debug everytime. I’ve tried using else null; commands and cant seem to figure out how to get the darn thing to stop stating the obvious, which is if you dont have a right elbow then of course you wont have a right hand either so on and so forth. I suppose its not a problem with functionality it just creates a messy debug log.
This is an example of the full to partial right arm detection and body part creation portion of the AI script, others run to check if there is still hands/elbows to determine which model to instantiate. Is there something obviously wrong in how I’ve arranged it? I’ve only been using unity for a couple weeks so a lot of it is new to me and I’m sure its glaring me in the face.
if(rarm)
{
var rarmcheck = transform.Find("Armature/Root/Bone_001/MidSpine/Bone_003/Bone_003_R/RightElbow");
if(!rarmcheck)
{
rarm = false;
if(!rarm && !rhand && !relb)
{
var ras = Instantiate(ram,transform.Find("Armature/Root/Bone_001/MidSpine/Bone_003/Bone_003_R/raspawn").transform.position, Quaternion.identity);
ras.transform.Rotate(20, 10, 10);
ras.rigidbody.velocity = ras.transform.down * 2;
//only bicep
}
if(!rarm && !rhand && relb )
{
var raes = Instantiate(raem,transform.Find("Armature/Root/Bone_001/MidSpine/Bone_003/Bone_003_R/raspawn").transform.position, Quaternion.identity);
raes.transform.Rotate(20, 10, 10);
raes.rigidbody.velocity = raes.transform.down * 2;
//bicep elbow and hand
}
if(!rarm && rhand && relb)
{
var raehs = Instantiate(raehm,transform.Find("Armature/Root/Bone_001/MidSpine/Bone_003/Bone_003_R/raspawn").transform.position, Quaternion.identity);
raehs.transform.Rotate(20, 10, 10);
raehs.rigidbody.velocity = raehs.transform.down * 2;
//whole arm
}
}
Any help or suggestions would be much appreciated, and if it really is a non issue then that would be great to hear as well since it seems as such.