Home > Mobile >  Trying to convert DateTime into int
Trying to convert DateTime into int

Time:01-16

so i have been trying to use both date time and int in the same if statement. my code is like this:

public int wantedHours, wantedMinutes;
public int sysHour = System.DateTime.Now.Hour;
public int sysMinutes = System.DateTime.Now.Minute;

void Update()
{
    if (sysHour == wantedHours && sysMinutes == sysMinutes)
    {
        sendTheNotif == true;
    } else
    {
        sendTheNotif == false;
    }
}

but it doesnt work. unity is giving me this error:

error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

CodePudding user response:

Integers are set correctly. You're using the equality operator in your if statements which is throwing the error Suggest reading the microsoft doc

Try this

(note, you are checking equality between sysMinutes == sysMinutes in your if statement, is that what you're wanting to do?)

public int wantedHours, wantedMinutes;
public int sysHour = System.DateTime.Now.Hour;
public int sysMinutes = System.DateTime.Now.Minute;

void Update()
{
    if (sysHour == wantedHours && sysMinutes == sysMinutes)
    {
        sendTheNotif = true;
    } else
    {
        sendTheNotif = false;
    }
}

CodePudding user response:

In your code, you did not assign a value to wantedHours, and wantedMinutes. also sysMinutes will always equal sysMinutes if there is no option to change its value in your code before comparison.

if (sysHour == wantedHours && sysMinutes == sysMinutes)

is the major problem. When you are comparing variables with == it means it should evaluate true if it is equal in type and value. So wantedHours has no value assigned according to your code block above. And sysMinutes is always equal to sysMinutes.

  •  Tags:  
  • Related