private void OnDestroy()
 {
     if (liftArrow != null)
     {
         UnityEngine.Object.Destroy(liftArrow);
         liftArrow = null;
     }
     if (dragArrow != null)
     {
         UnityEngine.Object.Destroy(dragArrow);
         dragArrow = null;
     }
     if (momentArrow != null)
     {
         UnityEngine.Object.Destroy(momentArrow);
         momentArrow = null;
     }
     legacyWingModel = null;
     stockAeroSurfaceModule = null;
 }
        void Start()
        {
            part.maximum_drag = 0;
            part.minimum_drag = 0;
            part.angularDrag = 0;
            if (HighLogic.LoadedSceneIsFlight)
                this.enabled = true;
            else if (HighLogic.LoadedSceneIsEditor)
                this.enabled = false;

            partLocalVel = Vector3.zero;
            partLocalForce = Vector3.zero;
            partLocalTorque = Vector3.zero;

            if (!part.Modules.Contains("ModuleAeroSurface"))
                part.dragModel = Part.DragModel.CYLINDRICAL;

            if(FARDebugValues.allowStructuralFailures)
            {
                FARPartStressTemplate template = FARAeroStress.DetermineStressTemplate(this.part);
                partStressMaxY = template.YmaxStress;
                partStressMaxXZ = template.XZmaxStress;
            }
            partTransform = part.partTransform;

            materialColorUpdater = new MaterialColorUpdater(partTransform, PhysicsGlobals.TemperaturePropertyID);
            if (part.Modules.Contains("FARWingAerodynamicModel"))
                legacyWingModel = part.Modules["FARWingAerodynamicModel"] as FARWingAerodynamicModel;
            else if (part.Modules.Contains("FARControllableSurface"))
                legacyWingModel = part.Modules["FARControllableSurface"] as FARWingAerodynamicModel;
            else
                legacyWingModel = null;

            // For handling airbrakes aero visualization
            if (part.Modules.Contains("ModuleAeroSurface"))
                stockAeroSurfaceModule = part.Modules["ModuleAeroSurface"] as ModuleAeroSurface;
            else
                stockAeroSurfaceModule = null;
        }
        private void FindWingInFrontOf()
        {
            if (start != StartState.Editor)
            {
                // Don't repeat traces until count expires or > 5 degree angle change
                if (LastInFrontRaycastCount-- > 0 &&
                    Vector3d.Dot(ParallelInPlaneLocal, LastInFrontRaycast) > 0.996)
                    return;

                LastInFrontRaycastCount = 10;
                LastInFrontRaycast = ParallelInPlaneLocal;
            }

            PartInFrontOf = null;
            WingInFrontOf = null;
            //PartInfrontOfName = "";

            if (ParallelInPlane != Vector3d.zero)
            {
                // Special case for control surfaces attached to a wing:
                // If the ray direction is within 60 degrees of up, just use parent.
                if (nonSideAttach > 0 && ParallelInPlaneLocal.y > 0.5 &&
                    (part.parent || start == StartState.Editor && part.potentialParent))
                {
                    Part parent = part.parent ?? part.potentialParent;
                    FARWingAerodynamicModel w = parent.GetComponent<FARWingAerodynamicModel>();

                    if (w != null)
                    {
                        PartInFrontOf = parent;
                        WingInFrontOf = w;
                        //PartInfrontOfName = PartInFrontOf.partInfo.title;
                        return;
                    }
                }

                RaycastHit hit = new RaycastHit();
                float distance = Mathf.Max((float)(3 * effective_MAC), 0.1f);
                RaycastHit[] hits = Physics.RaycastAll(CurWingCentroid, ParallelInPlane, distance, FARAeroUtil.RaycastMask);

                foreach (RaycastHit h in hits)
                    if (h.collider == part.collider || h.rigidbody == part.rigidbody)
                        continue;
                    else if (h.distance < distance)
                    {
                        distance = h.distance;
                        hit = h;
                    }

                if (hit.distance != 0)
                {
                    if (hit.collider.attachedRigidbody || start == StartState.Editor)
                    {
                        Part p = null;
                        if (hit.collider.attachedRigidbody)
                            p = hit.collider.attachedRigidbody.GetComponent<Part>();
                        if (p == null && start == StartState.Editor)
                            foreach (Part q in VesselPartList)
                                if (q.collider == hit.collider)
                                {
                                    p = q;
                                    break;
                                }
                        if (p != null && p != this.part)
                        {
                            if (start != StartState.Editor && p.vessel != vessel)
                                return;

                            FARWingAerodynamicModel w = p.GetComponent<FARWingAerodynamicModel>();

                            if (w != null)
                            {
                                PartInFrontOf = p;
                                WingInFrontOf = w;
                                //PartInfrontOfName = PartInFrontOf.partInfo.title;
                            }
                        }
                    }
                }
            }
        }