This is the code within the function I’m referencing. The for loop runs just as expected and when logging the variable within the for loop, it changes as expected. However, when the loop completes, the ‘int closest’ variable immediately reverts back to 0 as if the loop never ran. This happens the EXACT line after the loop. Changing the initial value of the variable in its declaration changes it successfully throughout the function, but not within the For-Loop. Any help would much be appreciated, I’ve been stumped for hours :,(
I don’t see anything in your code that would suggest that it would do that. I highly suggest, just littering your code with more Debug statements, specifically everywhere you change closest (right after line 16 and line 20), and try to figure it out from there.
On a side note: logic wise, your code wouldn’t work as you anticipate, since you never update winDist except on the first time, you should be updating it again every time you update closest.
My guess is index 0 of objsInRange is itself, which causes the “continue” to be called at line 9. This will result in winDist remaining at 1f for the first pass. On all subsequent passes, the “if” statement on line 18 will resolve to false unless line 11 results in a distance less than 1f. So the variable “closest” which was initialized to 0 on line 4 probably never actually changed.
But do what has been suggested by putting Debug.Log statements everywhere to figure out what is going on.
But if you just initialize winDist on line 2 to a ridiculously large number, I’d bet than your function magically starts just working.
Unity is single-threaded from your scripting standpoint so there’s no chance for any other script to even possibly be running at the same time, if that was your intention to lock out others from calling this same method simultaneously.
For any script that accesses the Unity API successfully, multi-threading will simply never be the case.
If this was a coroutine, there might be a calling for that sort of interlock, assuming you yield within the loop, but there are no yields above.
Also, these constructs are not a great idea… it will ignore ALL gameObjects with the same name, not just this one right here. Perhaps that is your intention, but if you only want it to ignore this one, that’s not what it will do. You might want to either compare the GameObject directly, or if you need something more debug-identifiable, perhaps use the .GetInstanceID() of each one for unique-ness.
I’m sorry I forgot to mention, this is an incomplete function, so don’t worry about that aspect, it should still be returning a different value than it is. So I already have my two main debug.logs, the first within the for-loop. It would print a value during the last loop iteration that differed from my return value. That is my problem.
I would agree usually, but my game objects are instantiated with unique names in this instance. Also, I understand the difference between functions and coroutines, it was just so I could track whether or not my function was running at all. I had a previous problem that I solved where my function was not running as it was supposed to. It’s left-over code from a stressful day lol.
Lmao, you think just like me. I got so fed up, I already literally tried all of these. I even put ‘closest = 1’ at the end of the for loop, and it straight up ignored it and set it back to 0 when it exited the for-loop.