- Home /
Help with If statement INSIDE if statement!!
Hey everybody! I'm having a problem scripting this feature in my game. Basically what I am trying to implicate is when you press a button, (which in my case is "c") it activates a component called "Screen Overlay" that creates a camera lens looking effect. I got that all working and all, but the next part I wish to add is night vision. I want it to be where IF the camera lens is on, so if "c" has been pressed then you can press "n" to activate night vision, in which I'm trying to use another component called "Color Correction Curves" Here is a look at my script! Hopefully someone can help me!
function Start () {
GetComponent("ColorCorrectionCurves").enabled = false;
GetComponent("Fisheye").enabled = false;
GetComponent("GrayscaleEffect").enabled = false;
GetComponent("ScreenOverlay").enabled = false;
}
function Update () {
if (Input.GetKeyDown("c"))
{
GetComponent("GrayscaleEffect").enabled = !GetComponent("GrayscaleEffect").enabled;
GetComponent("ScreenOverlay").enabled = !GetComponent("ScreenOverlay").enabled;
GetComponent("Fisheye").enabled = !GetComponent("Fisheye").enabled;
if (Input.GetKeyDown("n"))
{
GetComponent("ColorCorrectionCurves").enabled = !GetComponent("ColorCorrectionCurves").enabled;
}
}
}
Answer by KiraSensei · Jul 28, 2014 at 08:01 AM
You will have to use another trick, something like that :
var lensIsOn:boolean = false;
function Start () {
GetComponent("ColorCorrectionCurves").enabled = false;
GetComponent("Fisheye").enabled = false;
GetComponent("GrayscaleEffect").enabled = false;
GetComponent("ScreenOverlay").enabled = false;
}
function Update () {
if (Input.GetKeyDown("c"))
{
lensIsOn = !GetComponent("GrayscaleEffect").enabled;
GetComponent("GrayscaleEffect").enabled = lensIsOn;
GetComponent("ScreenOverlay").enabled = lensIsOn;
GetComponent("Fisheye").enabled = lensIsOn;
}
if (lensIsOn && Input.GetKeyDown("n"))
{
GetComponent("ColorCorrectionCurves").enabled = !GetComponent("ColorCorrectionCurves").enabled;
}
}
And to have better performances, I would suggest you to stock GetComponent(...) in variables because it is a very heavy function, so it can slow down your game if the user hits the "c" key very often.
Thank you $$anonymous$$iraSensei! Your script was exactly what I was looking for!
Answer by dsada · Jul 28, 2014 at 08:01 AM
im not sure that this is your problem. If you want to press the "n" button while the "c" is held down you need to write this:
if(Input.GetKey(KeyCode.C))
{
if(Input.GetKeyDown(KeyCode.N))
{
//this code is executing if the N is pressed while C is held down
}
}