GUI question, execution every frame?

hi, i have this code

function OnGUI(){

        toggleFadeIn=GUI.Toggle(Rect(5,Screen.height-25,400,30),toggleFadeIn,"Toggle Fade In/Out");

        if(toggleFadeIn){

            SceneControl.EnableFadingOut(true);
            Debug.Log("True");

        } else {

            SceneControl.EnableFadingOut(false);
            SceneControl.RestoreTransparency();
            Debug.Log("False");

        }

}

it works, but the problem is that it is called every frame, Debug.Log show it. it is not necesarry this code to be called every frame. where should i put it then?

I don't think it really matters unless EnableFadingOut is an expensive function. What you can do is have the function called every 2 frames or every 10 frames by checking that the current frame count leaves no remainder when divided. e.g.

if(toggleFadeIn && Time.frameCount % 2 == 0)

alternatively if you want it every 10 frames:

if(toggleFadeIn && Time.frameCount % 10 == 0)

i managed to get it working by using GUI.changed

if(GUI.changed){

            if(toggleFadeIn){

                SceneControl.EnableFadingOut(true);
                //Debug.Log("True");

            } else {

                SceneControl.EnableFadingOut(false);
                SceneControl.RestoreTransparency();
                //Debug.Log("False");

            }
        }

we have execution only if the user interacted with GUI