Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by jimmyismike · Aug 15, 2011 at 12:09 PM · disappearcommandonappear

Object appear and disappear on command

How can i set it so if i press 1 a object appears and if i press 1 again it leaves.

K here what i have so far.

 function Update () {
     if (Input.GetKeyDown ("1"))
       renderer.enabled = !renderer.enabled;
     if (Input.GetKeyDown ("1"))
        renderer.disabled = !renderer.disabled;
  }

like this?

Comment
Add comment · Show 8
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 CHPedersen · Aug 15, 2011 at 01:37 PM 0
Share

Excellent. :) Thanks for editing it up here. Now, as for the code you've posted... First off, you have two update functions. That's no good. The second Update function is the one from the example in the documentation I linked to, you've just copypasted that in there; you need to delete that, since turning something on and off once per second isn't what you want.

avatar image CHPedersen · Aug 15, 2011 at 01:38 PM 0
Share

After you've deleted the Update function from the documentation, make sure your curly braces ("{" and "}") match and line up properly. Curly braces delimit one block of code. See my updated answer for example.

avatar image jimmyismike · Aug 15, 2011 at 01:41 PM 0
Share

$$anonymous$$ i fixed curly braces but i still have trouble

avatar image jimmyismike · Aug 15, 2011 at 01:44 PM 0
Share

$$anonymous$$ i updated it again to your example.

avatar image CHPedersen · Aug 15, 2011 at 01:47 PM 0
Share

That's already much better, but there's still unnecessary stuff in there. First of all, you haven't deleted the Update-function from the documentation yet. You only deleted the function declaration, i.e. the line that read "function Update()". You need to remove its body as well. Look at the comments in there - it says "Find out whether current second is odd or even" right inside your code. Do you need that? Nope. Delete those lines of code, and the curly-braces that surround them.

Show more comments

1 Reply

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

Answer by CHPedersen · Aug 15, 2011 at 12:14 PM

Poll Input.GetKeyDown every frame, see documentation here:

http://unity3d.com/support/documentation/ScriptReference/Input.GetKeyDown.html

Then when it returns true, set the renderer of the object to enabled/disabled, see documentation here:

http://unity3d.com/support/documentation/ScriptReference/Renderer-enabled.html

Here's an example of something that could work in your case:

function Update () {
    if(Input.GetKeyDown("1"))
        renderer.enabled = !renderer.enabled;
}
Comment
Add comment · Show 7 · 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 jimmyismike · Aug 15, 2011 at 12:29 PM 0
Share

Sorry i'm really nooby can you type out codes for me. I'm confused and getting errors. I'm still learning java and still in the basics. Excuse my negligence please.

avatar image CHPedersen · Aug 15, 2011 at 01:04 PM 0
Share

Nope. :-D Sorry, but I'm not going to simply code it for you. I'd like for you to try harder, yourself. Show me what you've done so far, by updating your original question with the code you've written, and try to explain why and where it fails in as much detail as possible. Then I'll help with that.

avatar image jimmyismike · Aug 15, 2011 at 01:28 PM 0
Share

ok, wait it might be a bit.

avatar image jimmyismike · Aug 15, 2011 at 01:28 PM 0
Share

I make it as a answer.

avatar image CHPedersen · Aug 15, 2011 at 01:29 PM 0
Share

No, don't do that. If it's not an answer to your question, don't post it as one. Update your original question with it ins$$anonymous$$d.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiplayer - Standing Still Makes Player Disappear 0 Answers

My model is keep disappearing or not visible properly in game mode on first person controller: 0 Answers

how to make a button call another button and then disappear 1 Answer

Make a platform disappear after the next one appear 2 Answers

Realize toroidal movement 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