SetActive delay between code and actual object on the screen
My question is fairly basic, however, I'm having difficulty in finding an answer. I've broken down my actual problem as simple as I can in the code below. In my scene I have a single button, that when pressed, calls my ToggleImage() method below 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.
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 right after the if...else statement in my ToggleImage() method. 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 if..else statement 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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActiveTest : MonoBehaviour
{
public GameObject image;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void ToggleImage()
{
if (image.activeInHierarchy) { image.SetActive(false); }
else { image.SetActive(true); }
}
}
Same problem in Unity 2019. There’s at least a 3 second delay between when the code calls the SetActive property of my object and when it actually goes inactive.
Your answer
Follow this Question
Related Questions
I am trying to close the GUI if its already active and the user clicks 0 Answers
Can't use SetActive (true) on collision 0 Answers
add button to switch cars 1 Answer
How do you activate a game object ? 1 Answer
How to keep gameObjects set as triggers to only trigger once, even after a scene reload? 0 Answers