• 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
0
Question by WilliamFerdinand · May 20, 2015 at 09:22 AM · physics

How do I show/hide gameobjects through script?

I have a gameobject that needs to be shown when I press the key "i". I have been stuck on it for a while now. I have tried using " set active = true/flase" However this doesn't seem to work and the console tells me that it needs to get the API's which I allow however their nothing is working. You guys have any ideas that that is new or can I improve my script. Thank you in advance. :)

Comment
Add comment · Show 2
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 KiraSensei · May 20, 2015 at 09:22 AM 0
Share

Please post your code.

avatar image WilliamFerdinand · May 22, 2015 at 09:21 AM 0
Share

Hi, I have tried your suggestions and they don't seem to work. In the script " GetComponent..." the console says nothing is wrong yet nothing happens. Here's my script:

 void Update () {
 
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Z))
     {
 
         GetComponent<Renderer>().enabled = true;

     }

     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.X))
     {
         
         GetComponent<Renderer>().enabled = false;

         
     }

I have no idea whats wrong. However are there any components I should add to my character. I think that is the main reason Its not working because it has nothing attached to it. Thank you so much in advance. You responded so quickly and I really appreciate it.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Halfbiscuit · May 20, 2015 at 09:36 AM

Well in C# you can use gameObject.SetActive(false/true) to enable or disable the object.

If you are listening for inputs in the Update function you might just want to disable the renderer using:

 GetComponent()<Renderer>.enabled = true/false;
Comment
Add comment · Show 1 · 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 Menzle · Apr 19, 2020 at 05:09 PM 0
Share

thanks, but when I want to enable/disable any other gamobjects trough this script, how does that work? I've tried to put the name on it, but it didn't work. any ideas?

avatar image
1

Answer by sgriff08 · May 20, 2015 at 11:04 AM

See the linked thread, I think you will find it useful :)

http://answers.unity3d.com/questions/14165/show-and-hide-a-prefab-or-gameobject.html

Comment
Add comment · Show 1 · 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 Halfbiscuit · May 20, 2015 at 11:54 AM 0
Share

This answers page is using unity 4, if you want to access a renderer in Unity 5 you will need to use GetComponent.

gameObject.renderer

avatar image
0

Answer by Arshww · Apr 19, 2020 at 06:46 PM

Hello, let me suggest you this:

 ///This script must be attached on a gameobject wich won't be deactivated
 //If you attach this script to gameobject wich will be deactivated then will stop working
 
 public GameObject _Object; // Attach your GameObject you'd like to active/deactive in the inspector
 bool isRendererEnabled = true; //This var is only useful if you are working activating/deactivating gameobject's renderer
 
 void Update()
 {
     //Code below is if you'd like to activate/deactivate the whole GameObject || DELETE IT IF YOU PREFER CODE BELOW
     if(_Object.activeInHierarchy && Input.GetKeyDown(KeyCode.I)) //Check if GO is active and if pressed key i
     {
         _Object.SetActive(false); //If so, then deactivate it
     }else if(!_Object.activeInHierarchy && Input.GetKeyDown(KeyCode.I)) //Check if GO is deactivated and if pressed key i
     {
         _Object.SetActive(true);//If so, then activate it
     }
 
     //And the code below is if you'd like to activate/deactivate only gameobject's renderer || DELETE IT IF YOU PREFER CODE ABOVE
     if(Input.GetKeyDown(KeyCode.I) && isRendererEnabled)
     {
         _Object.Component.GetComponent<Renderer>().enabled = false;
         isRendererEnabled = false;
     }else if(Input.GetKeyDown(KeyCode.I)&& !isRendererEnabled)
     {
         _Object.Component.GetComponent<Renderer>().enabled = true;
         isRendererEnabled = true;
     }
 
 }
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

7 People are following this question.

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

Related Questions

2D 360 degress platformer example needed 0 Answers

Simulating the Character Controller? 0 Answers

Icy / Slippery surface? 1 Answer

Wheel Collider Tilt? 1 Answer

Realistic Helicopter AI Movements using physics 0 Answers


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