• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by naive231 · Mar 05, 2013 at 01:41 AM · gameobjectrendereractive

How can I hide a GameObject without active=false?

As far as I know, there are two ways to hide a GameObject.

The most popular way is GameObject.SetActive(false); But this method will disable all functions of it, and that is not my option.

Second way that I didn't successfully make it worked is GameObject.renderer.enable = false; Because I always got error message said : "MissingComponentException: There is no 'Renderer' attached to the "island01" game object, but a script is trying to access it." when I assign false to it. I don't understand why. There is a renderer object in GameObject indeed. Why I always got this error message?

I just want hide GameObject without set active property false. I believe that renderer.enable should be the answer, but I just can't assign value to it. Anybody knows the way?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Mar 05, 2013 at 05:27 AM 1
Share

FWIW note that in some games, you very simply move it off screen. depending on the situation this can be faster.

avatar image Eric5h5 · Mar 05, 2013 at 05:32 AM 1
Share

I don't think that can ever be faster. If you move it offscreen it still needs to be culled, whereas disabling the renderer means it's no longer part of the rendering process at all.

avatar image kskjadav007 · Jun 27, 2018 at 06:08 AM 0
Share

you can set it's scale to zero

4 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Eric5h5 · Mar 05, 2013 at 01:56 AM

GetComponent<Renderer>().enabled = false is correct. If Unity tells you that there's no renderer attached to that object, then you can be 100% sure that there is no renderer attached to that object. Most likely the renderer is on a child.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image naive231 · Mar 05, 2013 at 02:35 AM 1
Share

I think my description lost some important conditions. I assign renderer.enable property on a GameObject that contain a NGUI's UISprite object only. So I tried parent object and child object both, and all them gave me the same error message. $$anonymous$$y thought is maybe both of them do not have renderer component indeed. Finally I set UISprite's enable property to false, and it works as expected. So, is it an answer? XD

avatar image Eric5h5 · Mar 05, 2013 at 02:41 AM 0
Share

Yes, in that case the error was because there was no renderer component at all. :)

avatar image
16

Answer by illustir · Oct 21, 2015 at 02:30 PM

I found fiddling with the renderer to be not foolproof across 2D/3D.

My solution which looks to be fairly solid is to set the scale to [0] (and in the normal case to work with a scale that is [1]).

 // Hide button
 GameObject.Find ("ShareButton").transform.localScale = new Vector3(0, 0, 0);

 // Show button
 GameObject.Find ("ShareButton").transform.localScale = new Vector3(1, 1, 1);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image engri · Sep 21, 2016 at 10:20 AM 0
Share

thanks. This is the solution i need.

avatar image vikiridi · Jun 27, 2018 at 03:37 AM 0
Share

GENIUS! Absolutely GENIUS! I love this solution! Just what I wanted!!! How did I not think of this myself....

avatar image Nixtheworld · Aug 11, 2019 at 11:50 PM 0
Share

This is brilliant! Thank you soooo much.

avatar image fafase · Aug 12, 2019 at 06:19 AM 0
Share

One side effect, object is not visible but is likely to be kept in the rendering process. Not sure if Unity discard when scale is 0 (doubtful). $$anonymous$$oving out would result in object being out of camera frustum so not rendered.

avatar image
1

Answer by RowanG1 · Mar 01, 2018 at 09:48 AM

Temporarily moving the object to behind the camera might be a solution.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ajwesson0 · 2 days ago

As @Eric5h5 said, GetComponent<Renderer>().enabled = false is correct, but this will only work for 3D objects. If you are trying to do this with UI, then you will want to use GetComponent<CanvasRenderer>().cull = false. (Or if you don't know which your object is, try both and check the result of GetComponent.)

(Setting the scale to 0 will technically get what you want, but only if you don't care about performance because the renderer is still going to try and render it. Similarly, moving the object outside of the camera's view still means the culling logic needs to do math to realize it should be culled.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to make my gameobjects disable and enable? 2 Answers

Child object of another GameObject invisible or inactive @ runtime 2 Answers

What do the "active" and "static" checkboxes do? 1 Answer

Deactivate an object - and all scripts in that object deactivated? 1 Answer

How do I assign a renderer to a game object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges