I want use GUI code to my games, but the code was not working properly.
Try not to use OnGUI(). It’s the legacy system. It works but it has a ton of different limitations and gotchas. It will cause you far more headaches in the future.
Instead use the new UnityEngine.UI class of objects, which you can create in the hierarchy: right click → UI in hierarchy.
thank you very much, I know the UI class. I want to try use OnGUI(), but if it will cause me more headaches in the future, I will leave it.
What is left of the legacy GUI system is now referred to as the Immediate Mode GUI (IMGUI). It is no longer intended to be used to create in game UI’s for players to interact with. Instead it is intended for making developer facing debugging menus or displaying debugging info - something the end user should never have to interact with.
As such you should expect to run into problems using it for anything more than what is currently intended, since Unity isn’t testing it for anything more than that anymore.