- Home /
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?
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.
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.
$$anonymous$$ i fixed curly braces but i still have trouble
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.
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;
}
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.
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.
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.
Your answer
Follow this Question
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