- Home /
InvokeRepeating inside if statement
Hi,
I am trying to make my invoke repeating work, depending on the value of the variable TimeSelector, but it only activates on game start and then doesn't repeat.
EDIT: So I have now updated the code, so that the InvokeRepeating value is changed when the buttons are changed, but now InvokeRepeatingVAR gets stuck set to 15, and won't change.
The code is:
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.Collections.Generic;
public class MainGameScript : MonoBehaviour {
DateTime GameTime;
public Text DateDisplayer;
public Text Month;
public Text Year;
public Boolean MediumTimeIsSelected;
public Boolean FastTimeIsSelected;
public GameObject MediumTimeArrow1;
public int UpdateDateCounter;
public int MonthNumber;
public int YearNumber;
public int counter;
public string TimeSelector;
public int InvokeRepeatingVar;
public int CancelInvokeDecider;
void UpdateDate()
{
GameTime = GameTime.AddDays(7);
counter++;
Debug.Log("workingTime");
}
// Use this for initialization
void Start () {
GameTime = System.DateTime.Now;
}
//Update is called once per frame
void Update()
{
if (counter == 4)
{
MonthNumber++;
counter = 0;
}
if (MonthNumber == 12)
{
MonthNumber = 1;
YearNumber++;
MonthNumber = 0;
}
DateDisplayer.text = GameTime.ToString("dd-MM-yyyy");
Month.text = MonthNumber.ToString();
Year.text = YearNumber.ToString();
TimeSelector = PlayerPrefs.GetString("TimeSelector");
if (TimeSelector == "FastTime")
{
InvokeRepeatingVar = 15;
if (!IsInvoking("UpdateDate"))
{
InvokeRepeating("UpdateDate", InvokeRepeatingVar, InvokeRepeatingVar);
}
}
//==---------------------------------------------
if (TimeSelector == "MediumTime")
{
InvokeRepeatingVar = 30;
if (!IsInvoking("UpdateDate"))
{
InvokeRepeating("UpdateDate", InvokeRepeatingVar, InvokeRepeatingVar);
}
}
//------------------------------------
if (TimeSelector == "NormalTime")
{
InvokeRepeatingVar = 60;
if (!IsInvoking("UpdateDate"))
{
InvokeRepeating("UpdateDate", InvokeRepeatingVar, InvokeRepeatingVar);
}
}
Debug.Log(InvokeRepeatingVar);
Debug.Log(TimeSelector);
CancelInvokeDecider = PlayerPrefs.GetInt("CancelInvoke");
if (CancelInvokeDecider == 1)
{
CancelInvoke("UpdateDate");
CancelInvokeDecider = 0;
}
}
private void invoke(string v)
{
throw new NotImplementedException();
}
}
what's the value of InvokeRepeatingVar
?
check the syntax of InvokeRepeating
- the first variable is the delay in seconds from start, then next is the repeat interval. if they're zero, it's likely that it's working as programmed although not intended ;)
EDIT: if you wish to alter the repeat rate, you'll need to cancel the repeating then restart it with new values.
I have now changed the code, but the value won't change when the different speed settings are changed.
Answer by DcoltGaming · Oct 13, 2016 at 08:16 PM
Used Isvoking to check if invoking, and code then worked.