How I can add a delay to my script? (still no correct solution)

Hi, I have now a problem:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KeyDebug : MonoBehaviour
{
	private static System.Timers.Timer aTimer;
	public float RefreshTime;
	
	void Update () 
	{
		print(Input.inputString);	
	}
}

This code works properly, but the problem is that I need to add a refresh rate (in seconds) to the print stantment, because refresh too fast, makes lag and spams the console,
Thanks.
Also, sorry for my bad english.

2 Answers

2

The way that i’ve been doing it, although not so pretty but is simple is by creating timers. So by creating a timer() method, then adding by calling it in the update method you can add a private variable (time), which adds a second each time, adding Time.deltaTime. This would be an example using your code as well.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; 
     public class KeyDebug : MonoBehaviour
     {
     private int time;
     
     void Update () 
     {
         time += Time.DeltaTime;
         timer();
         //print(Input.inputString);    
     }void timer(){
        if (time >= (PUT TIME LIMIT HERE)){
            //Do whatever you want after a set time limit here
        }
    }

If you’re trying to reset a variable like a print statement, then you can just set the statement to be " " or null in the timer statement.

Thanks but doesn't works: Assets/Scripts/C Sharp/KeyDebug.cs(11,24): error CS0117: UnityEngine.Time' does not contain a definition for DeltaTime'

I ended up doing it alone, and I made it work, with a very short code. And most importantly of all, it worked.
I think we can close this up.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class KeyDebug : MonoBehaviour
{
     
    void Update () 
    {
        if (Input.anyKeyDown)
		{
			print(Input.inputString);	
		}    
    }
}