Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
1
Question by unity_CiQXbV-tBDunOQ · Jul 17, 2021 at 10:39 PM · togglekeypress

Toggle with key input only turning off

Very noob question and I've tried to see if this was answered somewhere else but couldn't find a solution.

This is the code I'm using. The problem I'm having is that the object disappears, but when I press the assigned key again, it won't reappear which is what I'd like for it to do.

I don't know much about coding and I'm just starting to learn.

 public GameObject glasses;
     // Update is called once per frame
     void Update()
     {
      if (Input.GetButtonDown("glassesKey"))
          glasses.SetActive(!glasses.activeSelf);
         
     }

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 Eno-Khaon · Jul 17, 2021 at 11:21 PM 0
Share

Is this script attached to the "glasses" GameObject itself? If so, an inactive GameObject won't run its Update() function.

For reference, you could also give yourself some more feedback in the console to ensure things are working as expected:

 void Update()
 {
     if(Input.GetButtonDown("glassesKey"))
     {
         glasses.SetActive(!glasses.activeSelf);
         Debug.Log(string.Format("{0}: \"glassesKey\" pressed: {1}.activeSelf = {2}", gameObject.name, glasses.name, glasses.activeSelf));
     }
 }

Edit: gave example a little more detailed diagnostic output

avatar image unity_CiQXbV-tBDunOQ Eno-Khaon · Jul 19, 2021 at 04:06 PM 0
Share

Thanks! I'll try adding the debug and see what it give me!

Yes, it's attached to the glasses GameObject itself but I think I also tried it on a separate game object just for the script

avatar image unity_CiQXbV-tBDunOQ Eno-Khaon · Jul 19, 2021 at 04:13 PM 0
Share

Yep that was the problem lol. Ty! When I made a game object with only the script before I had stupidly parented it to the object I was turning off XD

1 Reply

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

Answer by DrHerringbone · Jul 18, 2021 at 06:56 AM

Quick question is this script on the glasses object?

If so, you are disabling the object that looks for your key input! Try putting this script on the camera.

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 unity_CiQXbV-tBDunOQ · Jul 19, 2021 at 04:07 PM 0
Share

Oh! I will try this out and get back to you! Makes a whole lot of sense!!!

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.

Follow this Question

Answers Answers and Comments

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

toggle button pproblems 0 Answers

How to manage toggle (and other controls) state in a custom inspector? 2 Answers

Best way to speed up a flashing block? 1 Answer

My code works but produces an error in the console, not sure what's wrong but I would like to fix it. 1 Answer

Gui Toggle help 2 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