The question is answered, right answer was accepted
Combine If Statement Conditions
I changed the title after some Googling. But Yesterday I was toying around with the idea of having my if statement in my Switch case not only do something when the light slider reached a certain value but also only if the time on the computer was between a certain time of day. I was looking into I think it's called "Nested" if Statements? Not sure if that is what I should be looking at, but my head has been just spinning with ideas for combining the LDR Input and cross referencing that with Temperatures data, time of day and so one to execute different responses based on different values... had no idea sos much of this was possible.
I commented out the part I was experimenting with. It did not give me any console errors but did not work so I must be using it wrong although I thought I was on the right track with it??
(slider.value > 0.9f && oldValue <= 0.9f & sysHour >= 15 && sysHour <= 06) //Voice Will Only Activate between 6PM & 6AM
This part has more of the code block I was playing around with.
// else check if we need to play a sound and do it
if (slider.value > 0.9f && oldValue <= 0.9f) //This will call the voice no matter what time of day or night it is
//(slider.value > 0.9f && oldValue <= 0.9f & sysHour >= 15 && sysHour <= 06) //Voice Will Only Activate between 6PM & 6AM
StartCoroutine(BrightnessResponse());
else if (slider.value < 0.10f && oldValue >= 0.10f) //----------this has changed
//source.PlayOneShot (DarknessAudioClips [Random.Range (0, DarknessAudioClips.Length)]);
StartCoroutine(DarknessResponse());
break;
Answer by allenallenallen · Jan 09, 2016 at 04:04 PM
Well, looking at your first line of code. You can't have a military time that's greater than 15 and less than 6 at the same time.
slider condition stuff && (sysHour >= 15 && sysHour <= 06) // An integer can never fit both requirements at the same time.
Given that you're using the DateTime.Hour property: https://msdn.microsoft.com/en-us/library/system.datetime.hour(v=vs.110).aspx
Perhaps you meant this?
slider condition stuff && (sysHour >= 18 || sysHour <= 6) // Where it's truly between 6 PM and 6 AM
Since the value is supposed to be in military time, from 0 to 23.
00:00 would be 12:00 AM
15:00 would be 3:00 PM (You made the mistake of using 15 in your code.)
18:00 would be 6:00 PM
23:00 would be 11:00 PM
@allenallenallen, Thanks man, I changed it to this and now it should only provoke a brightness detected response if the LDR senses those values and the actual system time is between 6P$$anonymous$$ & 6Am if I understand that right?
Now should I be casting my values for the time exactly like you have them at 00:00 for midnight and 15:00 for 3P$$anonymous$$ and so on or is the way I have it here valid too?
// else check if we need to play a sound and do it
if //(slider.value > 0.9f && oldValue <= 0.9f) //This will call the voice no matter what time of day or night it is
(slider.value > 0.9f && oldValue <= 0.9f && (sysHour >= 18 || sysHour <= 06)) //Voice Will Only Activate between 6P$$anonymous$$ & 6A$$anonymous$$
StartCoroutine(BrightnessResponse());
else if (slider.value < 0.10f && oldValue >= 0.10f) //At These Values activate a "Darkness Detected" response
StartCoroutine(DarknessResponse());
break;
The way you do it is fine. I just say the :00 to explain the time thing. They should all be integers.
There is one thing though. I think it's better to write 6 ins$$anonymous$$d of 06. I'm not sure if 06 would give an error but it's best to keep integers simple.
@allenallenallen, Thanks man some points co$$anonymous$$g your way :) Now I can't wait till 6P$$anonymous$$ just to see if it works lol. Like I couldn't just tweak a temporary value ;) I did already and he seems to work. Now $$anonymous$$ITT will only say he detects light if it is between 6 P$$anonymous$$ and 6 Am :)
Follow this Question
Related Questions
Get Temperature Value and Prompt an Action 0 Answers
Can Someone Explain Why Message does not show Anything In Inspector 0 Answers
Can't Locate gameobject in array 3 Answers
problem with lists 0 Answers
Infinite Time inbetween if statements? 2 Answers