I tried asking this question, but maybe I was in the wrong forum, so I’m posting it here. Hopefully this is the correct location.
My question is fairly basic, however, I’m having difficulty in finding an answer. In my scene I have a single button and a single image. When the button is pressed, it calls a method which just turns the image on or off whenever the button is pressed. Really simple, and everything works. I do this by using the SetActive() method on the image. Basically, the image toggles between being visible and invisible.
I’ve noticed, however, that the actual image on the screen does not become active/inactive right when the code is executed. I’ve tested it by setting a break point in the method that toggles the visibility of the image. When running the debugger and pressing the button the code stops at the break point that I set as expected. The image starts out as active, so when I press the button for the first time it should become inactive and disappear from the screen. However, right after the statement in the code which sets the image inactive, the image remains visible on the screen. This is just causing some visual bugs in my actual game, but it doesn’t break the game play.
When calling SetActive, does the call get sent to some sort of queue that executes at some later time in the game loop? Is there a delay between setting an object active/inactive and when it actually happens on the screen?
My best guess is that the image’s active state changes in the Update function, which doesn’t line up exactly with when you call SetActive(). However, we are talking about milliseconds here, so I don’t see how this would affect the appearance of the game.
Well in my actual game (the example above is just a simplified version of the problem) I have several objects that when they reach a certain location they become inactive and then I reset their locations for preparation of when they become active again. When they become active again, they just do the same thing by moving to maybe some new location, become inactive, and reset their positions. To the player an object just simply moves to a location and disappears. However, I’ve noticed that after they reach their locations they will appear in their initial positions before becoming inactive. So if I have an object moving from x1 to x2, once it reaches x2 I briefly see it in x1 (which is it’s initial position), then it disappears instead of just having it disappear once it reaches x2. It’s supposed to reset back to x1 behind the scenes.
I have another solution that I’m using and it actually works better, but this has left me wondering if there’s something that I don’t understand about how calls are prioritized in Unity. I want to understand why this is happening, because it’s not what I would expect to happen.
Yes, I’m sure of the order. I double and triple checked. I even wrote a simplified version that took everything out of the scene except for a single object and a button. When you press the button, a method is called that just sets the object as inactive then moves it back to its initial position. In my code the image gets set as inactive first, then I reset the position, however, visually it appears to happen in reverse order even in this simplified version.
it shouldnt matter the order if its done in the same frame. so inside an update it shouldnt matter. or in a coroutine if you yielded between the two calls.
my guess would be its something else happening in another frame after those calls.