- Home /
missing component exception even though the component is there?
I'm getting this annoying error for missing component exception the strange thing is, it works fine and does exactly what I want the component is there as well on the object that is getting the difficulty this is the part of the script with problems:
Destroy (GetComponent(typeof(ConstantForce)));
Destroy (rigidbody);
just trying to remove the rigidbody, which requires removing the constantforce as well
Answer by hamstar · Sep 25, 2013 at 03:55 PM
You can't guarantee which component will be destroyed first, so the error probably occurs when the rigidbody is destroyed before the ConstantForce.
It's not very clean, but you could check if the ConstantForce is destroyed before you destroy the rigidbody. You would need to check every Update.
Update() {
if(GetComponent(typeof(ConstantForce)) == null) {
Destroy(rigidbody);
}
}
Although, if suits your needs it may be simpler just to disable the ConstantForce component rather than destroy it. I think enabling/disabling is instant.
GetComponent(typeof(ConstantForce)).enabled = false;
Destroy(rigidbody);
thanks for your help, I figured it out, it was another part of the script that was causing the issue :p
Your answer

Follow this Question
Related Questions
Adding a constant force to a barrel when i shoot it (some kind of high pressure that is eliberated) 3 Answers
hit.parent doesn't work when applied to physics GameObjects using Constant force 0 Answers
MissingComponentException: There is no 'GameObject' attached 0 Answers
Constant Force Script Changing 1 Answer