- Home /
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;
}
}
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!
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.
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' *
thank you so much! This works And now I know a new very handy trick ( || )
Your answer
Follow this Question
Related Questions
Hide object on key if finds object 0 Answers
Passing sender object as an argument in AddListener method 1 Answer
how to make object stay destroyed after OnTriggerEnter? 1 Answer
How to acsess the renderer of an Instantiate() 2 Answers
Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off -1 Answers