unity – Gizmos disappear after a scene transition

I have used OnDrawGizmos function to draw some cubes. If I run the project from that particular scene, the gizmos are visible. But after a scene transition they disappear. It appears again if I select the Gamobject that contains my script with the OnDrawGizmos function.

I tried adding the Selection.activeGameObject and it worked but I can’t build my project because unity editor is being involved in my scipt. Adding a Editor folder and keeping my scipt there does not allow me to drag and drop the script to the hierachy, so that I can attach the relavant gameobject that should be active.

I also tried #if UNITY_EDITOR … #endif, but after building the app, the app still did not show the gizmos on my phone.

Please can anyone let me know a way to stop gizmos from disappearing.