Making a counter that does not reflect on time

Hi All,

I’m working on making a counter that does not count time or a “score in the game”. I guess the best way to explain it as i want to count to 360 then back to 0, and continue counting. I have put a delay between each count so i can alter how fast it will count. I also have it updating a GUITEXT for debugging. I basically want to be able to trigger different events/animations at different time intervals.

I have tried a few options but most of the time unity will just crash. I have the function set up to a button to initiate for debug purposes.

Any help will be appreciated. Thanks

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Counter : MonoBehaviour
{

public Text counter;
private int count;
public int delay = 2000;
public Button startCount;

// Use this for initialization

void Start()
{
    count = 0;
}

public void StartCount()
{
    if (startCount == true)
    {
        Debug.Log("start count pressed");
        TimeCounter();
    }
}

void SetCountText()
{
    counter.text = count.ToString();

}
void TimeCounter()
{
    if (count != 360)
    {
        SetCountText();
        System.Threading.Thread.Sleep(delay);
        count = count + 1;
        TimeCounter();
    }

    if (count == 360)
    {
        System.Threading.Thread.Sleep(delay);
        count = 0;
        TimeCounter();
    }

}

}

So… You want to count to 360 every 2 seconds? Even Time.timeScale is 0? (When game pause)

Try this.

using UnityEngine;
using UnityEngine.UI;

public class Counter : MonoBehaviour
{
	public Text counter;
	public int count
	{
		get
		{
			return _c;
		}
		set
		{
			if (value == 360) //If it larger then 360.
			{
				value = 0;
			}
			_c = value;
			//You can do another thing here. As a counter is increese
			counter.text = value.ToString(); //Everytime this variable is change. It will automatic update your **counter Text**
		}
	}
	private int _c; //Part of Auto-Property
	public int delay = 2;//Originally your delay is 2000 miliseconds. But this one use second instead of millisecond.
	public Button startCount;
	public bool isCounting { get; set; } //Instead of counting forever. Atlease make it can stop ._.
	private float lastTime; //Threading alternative

	void Start()
	{
		count = 0;
		startCount.onClick.AddListener(delegate { StartCount(); }); //In case if you not assign it at inspector. Remove this if you already assign in inspector
		lastTime = Time.time + delay; //Threading sleep alternative
	}

	public void StartCount()
	{
		isCounting = !isCounting;
		if (isCounting)
		{
			Debug.Log("Start count pressed");
		}
	}

	void Update()
	{
		if (isCounting)
		{
			if (Time.time > lastTime) //Time.time is always update by unity.
			{
				//After delay reach
				count += 1; //Count up. (As you originall want;
				lastTime = Time.time + delay;
			}
		}
	}
}

@Toon_Werawat

Thanks for the help, it has got me closer to the goal. A few things I need to change.

I don’t want to count to 360 then wait 2 seconds, I had a larger time limit there just for debugging purposes. What I’m trying to achieve is for example an an engine rotation = 360 degrees, hence why the count is to 360.

I want the pause between each count so I can essentially alter the speed of the motor, preferably milliseconds.

so if I want the speed to be 1 revolution per minute the pause between each count + 1 would be 0.0027777777777778. So if I calculated right it should count to 360 every minute, with no pause between.

Eventually I want create events at certain points of the count. I will try work with the code you gave as it works a lot better then my original. Any more tips would be greatly appreciated.

Thank You