• 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 Daniel-Talis · Dec 24, 2018 at 09:04 AM · componentskeypress

EnableDisableComponents

I'm struggling to turn GameObjects off and on with a keypress. Unity shows an example with a light that can be toggled so I've attempted to convert their code.. not working. Am I completely up the wrong tree..? Any help..?

 using UnityEngine;
 using System.Collections;
 
 public class EnableComponent2 : MonoBehaviour
 {
     private Cube myCube;
 
 
     void Start()
     {
         myCube = GetComponent<Cube>();
     }
 
 
     void Update()
     {
         if (Input.GetKeyUp(KeyCode.F1))
         {
             myCube.enabled = !myCube.enabled;
         }
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by Hellium · Dec 24, 2018 at 10:07 AM

Turning on and off a GameObject is different from toggling a component.

If you want to toggle on and off an entire GameObject (not one of its component), attach the following script to an empty gameObject, not the gameObject to toggle

  using UnityEngine;
  using System.Collections;

   // Rename the file to GameObjectToggle.cs
  public class GameObjectToggle : MonoBehaviour
  {
      public GameObject GameObjectToToggle; // Drag & drop the gameObject to toggle in the inspector
      public KeyCode ToggleKeyCode = KeyCode.F1; // Change the keycode to use in order to toggle
 
  
      void Update()
      {
          if (GameObjectToToggle != null && Input.GetKeyUp(ToggleKeyCode))
          {
              GameObjectToToggle.SetActive( !GameObjectToToggle.activeSelf ) ;
          }
      }
  }


Comment
Add comment · Show 3 · 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 Daniel-Talis · Dec 24, 2018 at 10:29 AM 0
Share

Thanks Hellium, I followed your instructions above, there are no errors but the object doesn't toggle on and off for some reason. Can you see any reason why..?

avatar image Hellium Daniel-Talis · Dec 24, 2018 at 11:02 AM 0
Share

I realised I made a mistake in my code....

 GameObjectToToggle.SetActive( GameObjectToToggle.activeSelf ) ;

$$anonymous$$ust be

 GameObjectToToggle.SetActive( !GameObjectToToggle.activeSelf ) ;

If it still does not work, make sure:

  1. You have attached the script to a gameObject different from the one you want to toggle?

  2. You have attached the script on only one gameObject

  3. You have dragged & dropped the gameObject to toggle in the GameObjectToToggle field in the inspector

  4. You don't get any error in the console

avatar image Daniel-Talis · Dec 24, 2018 at 09:30 PM 0
Share

Working now, thanks for that.

avatar image
0

Answer by cdr9042 · Dec 24, 2018 at 10:04 AM

This is enabling/disabling the Cube component, I don't recommend doing this. You should use GameObject.SetActive instead. The switch script should be on a different object than the object you are turning on and off.

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

97 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 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 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 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 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

Key Press Queuing 2 Answers

Activate and deactivate sound when holding key 1 Answer

Instantiate a prefab with a key press to a relative location? 1 Answer

Disable Sprite when keypress (Working Script, just need disble) 0 Answers

getaxis and getkey don't work simultaneously 3 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