Question by
devinwc · Sep 28, 2020 at 10:52 AM ·
movementsetactiveif statementelse if
If & Else If with == and && not working
I'm very new to coding, so pardon my code if it's not the cleanest. I'm trying to activate a tool if it is highlighted AND if a button is pressed, which also deactivates the other tools. For some reason, it's only working if I only have the "==" statement without the "&&" statement. Code snippet below is what I've managed to put together so far. if(selectionRenderer != null) { selectionRenderer.material = highlightMaterial; //This works Debug.Log(selection.name + " Is Selected");
targetDevice.TryGetFeatureValue(CommonUsages.primaryButton, out bool primaryButtonValue);
if((Input.GetKeyDown(KeyCode.DownArrow)) && (selection == item1.transform)) //This is what I want to work
{
Debug.Log("Tool 1");
tool1.SetActive(true);
tool2.SetActive(false);
tool3.SetActive(false);
tool4.SetActive(false);
tool5.SetActive(false);
}
else if(selection == item2.transform) //This works, but not the result I want. It activates the correct tool, but doesn't require a button to be pressed.
{
Debug.Log("Activating Tool 2");
tool1.SetActive(false);
tool2.SetActive(true);
tool3.SetActive(false);
tool4.SetActive(false);
tool5.SetActive(false);
}
else if(selection == item3.transform)
{
Debug.Log("Tool 3");
if(primaryButtonValue || Input.GetKeyDown(KeyCode.P)) //This is another way I've tried formatting what I want to work, but still doesn't work
tool1.SetActive(false);
tool2.SetActive(false);
tool3.SetActive(true);
tool4.SetActive(false);
tool5.SetActive(false);
}
else if((selection == item4.transform) && (primaryButtonValue || Input.GetKeyDown(KeyCode.P))) //Another way of trying to format what I want it to do
{
Debug.Log("Tool 4");
tool1.SetActive(false);
tool2.SetActive(false);
tool3.SetActive(false);
tool4.SetActive(true);
tool5.SetActive(false);
}
else if(selection == item5.transform && (primaryButtonValue || Input.GetKeyDown(KeyCode.P))) //Same format as the one above
{
Debug.Log("Tool 5");
tool1.SetActive(false);
tool2.SetActive(false);
tool3.SetActive(false);
tool4.SetActive(false);
tool5.SetActive(true);
}
Any idea why the code isn't working? Or if there's a better way to format all this to make it work? Oh, and the || statements are for testing so I don't have to put on my VR headset every time.
Thank you so much for your help!
Comment