0

I'm having trouble with time in c#, I've created a stopwatch and I want the game to carry on for a set time period before stopping and assigning autoPlay to true. Instead what is happening is the game is stopping for the set time in Thread.Sleep - is there anything i can substitute this for, or a better way of doing this? code below.

void Timer()
{
    // Create new stopwatch.
    Stopwatch stopWatch = new Stopwatch();
    // Begin timing.
    stopWatch.Start();
    // Do something.
        Thread.Sleep(200);
    // Stop timing.
    stopWatch.Stop();
    autoPlay = true;
}
Backs
  • 24,430
  • 5
  • 58
  • 85
T.Evans
  • 13
  • 5
  • 1
    Strictly your question isn't a duplicate - but basically you're going about it in such an incorrect way that your question has no additional value above the question I linked (I don't mean to be blunt, but your code just makes no sense, sorry). The answer is to use (one of) the existing Timer classes - please see linked question for details. – RB. Apr 01 '18 at 20:31
  • 2
    Are you actually using Unity-Container by microsoft or are you using the Unity3d game engine? – Scott Chamberlain Apr 01 '18 at 22:35
  • If it's the game engine - use a coroutine, that's what they are there for. (containing `yield return new WaitForSeconds(0.2)`) – Manfred Radlwimmer Apr 01 '18 at 22:40

1 Answers1

0

One of the simplest ways of achieving what you want is to use Invoke

void Start(){

   Invoke("StartAutoPlay", 200);

}

void StartAutoPlay(){

    autoPlay = true;

}

Basically what this would do is to begin timer for 200 seconds on Start and then execute StartAutoPlay which will turn your bool to true. Read more on Invoke here: Link

Arthur Belkin
  • 125
  • 1
  • 10