Unity3D game development: how to hide objects and unity3d Game Development

Source: Internet
Author: User

Unity3D game development: how to hide objects and unity3d Game Development
To hide an object, you only need to call the setActive method of GameObject,


GameObject. setActive (false); // hide
GameObject. setActive (true); // display

However, sometimes the hidden object cannot be displayed again because the developer makes the GameObject a local variable, and the developer allows the current Find to be used for every control operation, however, the Find method can only Find objects whose setActive value is true. The Find function only returns the active game objects. Therefore, it cannot Find the control after hiding it, in this case, you cannot display it again. Dog planing Learning Network]

This is also a mistake I made. I used to like it in the past, but the API Manual also wrote:
For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject. FindWithTag.
We recommend that you do not use this function in each frame unless you have. You can use a member variable to cache the results at the beginning or use the GameObject. FindWithTag function.
Therefore, try not to develop the bad habit of using the current call every time.

Solution: you only need to set the GameObject as a global variable and Find it once in Awake (or a suitable location determined based on your needs), and directly reference it elsewhere, instead of performing the Find operation again.

I am a beginner. I just want to write a solution to my problem and leave a backup for myself. It can also help other beginners.
The problem may be too simple. I hope you will not try it out. If there are any errors, please point them out. Thank you very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.