- Home /
Assertion failed: Assertion failed on expression: 'IsActive() || GetRunInEditMode()' What does this mean?
I am using the code below and although it works perfectly i get this error whenever i press the button on my keyboard. Is this a bug of the engine or something wrong with my code?
private void OnGUI()
{
if (pistol_button)
{
if(GUI.Button(new Rect(20, Screen.height - 50, 100, 50), "1. Pistol") || Input.GetKeyDown(KeyCode.Alpha1))
{
child1.SetActive(true);
child2.SetActive(false);
}
}
if (shotgun_button)
{
if(GUI.Button(new Rect(140, Screen.height - 50, 100, 50), "2. Shotgun") || Input.GetKeyDown(KeyCode.Alpha2))
{
child2.SetActive(true);
child1.SetActive(false);
}
}
}
Answer by Aristonaut · Aug 02, 2019 at 09:58 PM
This seems to happen when calling SetActive()
in the OnGUI()
function. You could call SetActive()
in Update()
instead, or delay the deactivation till the end of the frame using a coroutine:
private void OnGUI()
{
...
StartCoroutine( SetActiveLater(false) );
...
}
private IEnumerator SetActiveLater(bool value)
{
yield return null;
gameObject.SetActive( value );
}
By delaying activations and deactivations till the next coroutine call, you won't get OnGUI being called in strange orders where your state has not fully propagated through (like where a dependent thing has it's OnGUI called first, then gets deactivated.)
Your answer
Follow this Question
Related Questions
Unity can't use the script. 0 Answers
How to make a or array of GUI.Button's? 1 Answer
Adding score when enemy dies (Errors) 2 Answers
Multiple Cars not working 1 Answer