Does anyone have a solution to this ? When I do the subtraction , it makes a loop that does not stop.

  if ((lamp.activeSelf == true)&&(test == true)) 
            money = money - 50;  // <---- This is the problem
            test = false;   
  • 3
    \$\begingroup\$ There doesn't look to be a loop going on here, this is just an If statement? Where is this held? Is it in the Update method? Could you post the full code please to help us understand the problem better? \$\endgroup\$
    – hobnob
    Jul 21, 2016 at 8:08
  • 1
    \$\begingroup\$ moreover is useless to explicitly compare a boolean with "true", just use lamp.activeSelf && test \$\endgroup\$
    – daveoncode
    Jul 21, 2016 at 8:17
  • \$\begingroup\$ Could you tell how did you find that there's a infinite loop? Does money continue to be subtracted? You can monitor money and test vars using debug.log() to see what is happening. \$\endgroup\$
    – klaymen
    Jul 21, 2016 at 8:57
  • 1
    \$\begingroup\$ I'm voting to close this question as off-topic because there is no loop. \$\endgroup\$
    – Vaillancourt
    Jul 21, 2016 at 10:03
  • \$\begingroup\$ Usually an infinite loop in Unity will manifest as the editor/player hanging, which the user doesn't mention, so I'd suspect what they mean is that the regular old game loop is continuing to trigger this code on many consecutive frames, without being stopped by test — suggesting that some code elsewhere is resetting test to true. Could also be that this is inside a setter for money itself (or a method called by that setter) so subtracting from money triggers a recursive call sequence. There'd still need to be something resetting test. We'll need to see more code & symptoms to diagnose. \$\endgroup\$
    – DMGregory
    Jul 21, 2016 at 11:13


Browse other questions tagged .