C# code executes instructions under if AND else

Hello guys,

i have the strangest problem i ever had since i am working with Unity. The thing is that in multiple if-else or if-else if-else statements the instructions under if AND else are called and i can see no error or pattern under which this strange and actually impossible thing happens. One example is this:

if (pointerRepresentation.contains(nearestEdgePixel))
{
   if (!m_currentSnapPoint.PointerOverElement)
        m_currentSnapPoint.OnPointerEnter(m_currentSnapPoint, LeapMotionManager.Instance.Pointer);                            
} else if (distancePointerToEdge <= m_snapToPointThreshold)
{
   if (!m_currentSnapPoint.PointerSnapped)
       m_currentSnapPoint.OnPointerSnappingStarted(m_currentSnapPoint, LeapMotionManager.Instance.Pointer, pointedImageViewComponent);
}
else
{
  if (!m_currentSnapPoint.InReachOfPointer)
       m_currentSnapPoint.OnSnapPointInReach(m_currentSnapPoint, LeapMotionManager.Instance.Pointer, pointedImageViewComponent);
  else if (m_currentSnapPoint.PointerOverElement)                                
       m_currentSnapPoint.OnPointerExit(m_currentSnapPoint, LeapMotionManager.Instance.Pointer);

  if (m_currentSnapPoint.PointerSnapped)
      m_currentSnapPoint.OnPointerSnappingEnded(m_currentSnapPoint, LeapMotionManager.Instance.Pointer, pointedImageViewComponent);
 }

Here the codes goes first into the outer if (pointerRepresentation.contains(nearestEdgePixel)) because the pointer contains the nearestEdgePixel and executes all it should but after this he goes into the last outer else, doesn’t check if (!m_currentSnapPoint.InReachOfPointer) and else if (m_currentSnapPoint.PointerOverElement) but goes instantly into the if (m_currentSnapPoint.PointerSnapped) instructions even if PointerSnapped is false at this exact moment (i tested it via break points). This and something like this i have in two other if-else statements. Any ideas how this can happen?

I wouldn’t become too focussed on whether step-through appears to be skipping lines - that’s more indicative of a bug in your debugger rather than a bug in your Unity code.

Let’s keep it simple - can you create a situation where Debug.Log() prints out values of variables that differ from those in the condition blocks necessary to reach that line of code? For example, try amending your code like this:

if (pointerRepresentation.contains(nearestEdgePixel))
{
    if (!m_currentSnapPoint.PointerOverElement) {
         Debug.Log("pointerRepresentation.contains(nearestEdgePixel) should be true and is " + pointerRepresentation.contains(nearestEdgePixel));
         Debug.Log("m_currentSnapPoint.PointerOverElement should be false and is " + m_currentSnapPoint.PointerOverElement);
         m_currentSnapPoint.OnPointerEnter(m_currentSnapPoint, LeapMotionManager.Instance.Pointer);
    }
 } 
 else if (distancePointerToEdge <= m_snapToPointThreshold)
 {
    if (!m_currentSnapPoint.PointerSnapped) {
        Debug.Log("pointerRepresentation.contains(nearestEdgePixel) should be false and is " + pointerRepresentation.contains(nearestEdgePixel));
        Debug.Log("distancePointerToEdge should be <= m_snapToPointThreshold. distancePointerToEdge is " + distancePointerToEdge + " and m_snapToPointThreshold is " + m_snapToPointThreshold);
        Debug.Log("m_currentSnapPoint.PointerSnapped should be false and is " + m_currentSnapPoint.PointerSnapped);
        m_currentSnapPoint.OnPointerSnappingStarted(m_currentSnapPoint, LeapMotionManager.Instance.Pointer, pointedImageViewComponent);
    }
 }
 else
 {
   if (!m_currentSnapPoint.InReachOfPointer)
        Debug.Log("pointerRepresentation.contains(nearestEdgePixel) should be false and is " + pointerRepresentation.contains(nearestEdgePixel));
        Debug.Log("distancePointerToEdge should be > m_snapToPointThreshold. distancePointerToEdge is " + distancePointerToEdge + " and m_snapToPointThreshold is " + m_snapToPointThreshold);
        Debug.Log("m_currentSnapPoint.InReachOfPointer should be false and is " + m_currentSnapPoint.InReachOfPointer);
        m_currentSnapPoint.OnSnapPointInReach(m_currentSnapPoint, LeapMotionManager.Instance.Pointer, pointedImageViewComponent);

   else if (m_currentSnapPoint.PointerOverElement)
        Debug.Log("pointerRepresentation.contains(nearestEdgePixel) should be false and is " + pointerRepresentation.contains(nearestEdgePixel));
        Debug.Log("distancePointerToEdge should be > m_snapToPointThreshold. distancePointerToEdge is " + distancePointerToEdge + " and m_snapToPointThreshold is " + m_snapToPointThreshold);
        Debug.Log("m_currentSnapPoint.InReachOfPointer should be true and is " + m_currentSnapPoint.InReachOfPointer);
        Debug.Log("m_currentSnapPoint.PointerOverElement should be true and is " +m_currentSnapPoint.PointerOverElement);
        m_currentSnapPoint.OnPointerExit(m_currentSnapPoint, LeapMotionManager.Instance.Pointer);
 
   if (m_currentSnapPoint.PointerSnapped)
        Debug.Log("pointerRepresentation.contains(nearestEdgePixel) should be false and is " + pointerRepresentation.contains(nearestEdgePixel));
        Debug.Log("distancePointerToEdge should be > m_snapToPointThreshold. distancePointerToEdge is " + distancePointerToEdge + " and m_snapToPointThreshold is " + m_snapToPointThreshold);
        Debug.Log("m_currentSnapPoint.PointerSnapped should be true and is " + m_currentSnapPoint.PointerSnapped);
        m_currentSnapPoint.OnPointerSnappingEnded(m_currentSnapPoint, LeapMotionManager.Instance.Pointer, pointedImageViewComponent);
  }

That’s how I interpret the conditions for each block, but you might want to check them carefully! Can you make this code print out a seeming anomaly?