• 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 valtteri_m · Dec 26, 2014 at 09:23 PM · javascriptrendererrendertogglegetkey

Render toggle help..

I have this script but it doesn't work.. I want so that if I press the left or right arrow the player becomes invisible

 function Update(){
     if(Input.GetKeyDown(KeyCode.LeftArrow)){
         renderer.enabled = false;
     }else{
         renderer.enabled = true;
     }
 
     if(Input.GetKeyDown(KeyCode.RightArrow)){
         renderer.enabled = false;
     }else{
         renderer.enabled = true;
     }
 }

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

1 Reply

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

Answer by gameplay4all · Dec 26, 2014 at 11:30 PM

If you wish to make a toggle, use this code:

 if(Input.GetKeyDown(KeyCode.LeftArrow)){
 renderer.enabled = !renderer.enabled;
 }

This will set the .enabled variable to the opposite of itself.

The problem with your previous code was that you used Input.GetKeyDown This method is only true for 1 frame and the next frame the script will run the else{} code and overwrite the code done in the previous frame.

You should use Input.GetKey(KeyCode.LeftArrow) instead.

Good luck!

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 valtteri_m · Dec 26, 2014 at 11:39 PM 0
Share

still didn't work, I tried all those things in different orders, I want that when left or right is being held down the player is invisible.

avatar image gameplay4all · Dec 26, 2014 at 11:56 PM 0
Share

You should use Input.Get$$anonymous$$ey this will be true as long as you hold the specified button.

Also don't use two separate if statements, make it 1. This won't overwrite the other if statement.

 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)){
  renderer.enabled = false;
 }
 else{
 renderer.enabled = true;
 }

I'm 100% sure this will work.

  • the || things mean 'or' *

avatar image valtteri_m · Dec 26, 2014 at 11:59 PM 0
Share

thank you so much! This works And now I know a new very handy trick ( || )

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

Hide object on key if finds object 0 Answers

Get all Render Objects in our current Scene with code (js) 1 Answer

renderer.enabled question 2 Answers

How can I show/hide all models in a GameObject? 1 Answer

How could I make the texture on one object follow the rotation of another object? 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