Why am I not able to use stopwatch.Restart()?

user3879250

I'm trying to call Restart() on a stopwatch instance but am getting the following error when trying to call it:

Assets/Scripts/Controls/SuperTouch.cs(22,59): error CS1061: Type System.Diagnostics.Stopwatch' does not contain a definition for Restart' and no extension method Restart' of type System.Diagnostics.Stopwatch' could be found (are you missing a using directive or an assembly reference?)

This is my code:

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

namespace Controls
{

    public class SuperTouch
    {
            public Vector2 position { get { return points [points.Count - 1]; } }
            public float duration { get { return (float)stopwatch.ElapsedMilliseconds; } }
            public float distance;
            public List<Vector2> points = new List<Vector2> ();

            public Stopwatch stopwatch = new Stopwatch ();

            public void Reset ()
            {
                    points.Clear ();
                    distance = 0;
                    stopwatch.Restart ();
            }
    }
}
DevEstacion

I'm guessing you are using .Net Framework 3.5 or below where the Restart method of Stopwatch doesnt exists.

If you want to replicate the same behavior you can do it like this.

Stopwatch watch = new Stopwatch();
watch.Start();
// do some things here
// output the elapse if needed
watch = Stopwatch.StartNew(); // creates a new Stopwatch instance 
                              // and starts it upon creation

The StartNew static method already exists on .Net Framework 2.0

More details about StartNew method here: Stopwatch.StartNew Method

Or alternatively, you can create an extension method for yourself.

Here is a mockup and usage.

public static class ExtensionMethods
{
    public static void Restart(this Stopwatch watch)
    {
        watch.Stop();
        watch.Start();
    }
}

Consume like

class Program
{
    static void Main(string[] args)
    {
        Stopwatch watch = new Stopwatch();
        watch.Restart(); // an extension method
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why am I able to use my value constructor even though I don't export it?

分類Dev

Javascript: if 'abstract' is a reserved word why I am still able to use it as an identifier?

分類Dev

Why am I not able to print 32.0 instead of 32 inspite of typeconversion?

分類Dev

Why am I not being able to connect to my database?

分類Dev

Why am I not able to decode this JSON with golang? It always prints an empty string

分類Dev

Why I am able to override Equals method if my class doesn't inherit from anything?

分類Dev

Why am I not able to build Vim with Visual Studio 2015 RC command line tools?

分類Dev

Using alpine source, why am I not able to source or "." my env file?

分類Dev

The touch event in my canvas is not working as intended and I am not able to identify the reason why it is not working

分類Dev

Why I am able to access the users of a different tenant without adding any API permission to application in Azure Portal?

分類Dev

I am not able to use start date and end date in clear airflow through CLI

分類Dev

Why am I forced to use !! in a combination of null checks?

分類Dev

Why use omnifaces while I am using primefaces?

分類Dev

I am trying to be able to take an uploaded image and make it the background image

分類Dev

I am not able to access variable value of another class in Java

分類Dev

I am in "Owner" group of sharepoint site but not able to edit/delete list

分類Dev

Not able to reset "changed" flag - what am I doing wrong?

分類Dev

How am I able to telnet to HTTP port 80?

分類Dev

How come I, as a normal user, am able to change ownership of a file?

分類Dev

Why am I getting an undefined?

分類Dev

Why am I getting this KeyError?

分類Dev

Why am I receiving a SyntaxError: invalid syntax regarding the use of an elif statement?

分類Dev

Why am I unable to use my Windows 8 after installing Windows 7 in a separate partition?

分類Dev

I can only connect internet to employers wifi. Apart from it, I am able to connect to wifi, but I am not getting internet

分類Dev

Why am I getting a NoClassDefFoundError in the pdfXFA example?

分類Dev

Why am I getting "svn: command not found"

分類Dev

Why am I not rendering my state to the screen?

分類Dev

Why am I getting an Inferred Latch Error?

分類Dev

Why am I getting this deprecated warning?! MongoDB

Related 関連記事

  1. 1

    Why am I able to use my value constructor even though I don't export it?

  2. 2

    Javascript: if 'abstract' is a reserved word why I am still able to use it as an identifier?

  3. 3

    Why am I not able to print 32.0 instead of 32 inspite of typeconversion?

  4. 4

    Why am I not being able to connect to my database?

  5. 5

    Why am I not able to decode this JSON with golang? It always prints an empty string

  6. 6

    Why I am able to override Equals method if my class doesn't inherit from anything?

  7. 7

    Why am I not able to build Vim with Visual Studio 2015 RC command line tools?

  8. 8

    Using alpine source, why am I not able to source or "." my env file?

  9. 9

    The touch event in my canvas is not working as intended and I am not able to identify the reason why it is not working

  10. 10

    Why I am able to access the users of a different tenant without adding any API permission to application in Azure Portal?

  11. 11

    I am not able to use start date and end date in clear airflow through CLI

  12. 12

    Why am I forced to use !! in a combination of null checks?

  13. 13

    Why use omnifaces while I am using primefaces?

  14. 14

    I am trying to be able to take an uploaded image and make it the background image

  15. 15

    I am not able to access variable value of another class in Java

  16. 16

    I am in "Owner" group of sharepoint site but not able to edit/delete list

  17. 17

    Not able to reset "changed" flag - what am I doing wrong?

  18. 18

    How am I able to telnet to HTTP port 80?

  19. 19

    How come I, as a normal user, am able to change ownership of a file?

  20. 20

    Why am I getting an undefined?

  21. 21

    Why am I getting this KeyError?

  22. 22

    Why am I receiving a SyntaxError: invalid syntax regarding the use of an elif statement?

  23. 23

    Why am I unable to use my Windows 8 after installing Windows 7 in a separate partition?

  24. 24

    I can only connect internet to employers wifi. Apart from it, I am able to connect to wifi, but I am not getting internet

  25. 25

    Why am I getting a NoClassDefFoundError in the pdfXFA example?

  26. 26

    Why am I getting "svn: command not found"

  27. 27

    Why am I not rendering my state to the screen?

  28. 28

    Why am I getting an Inferred Latch Error?

  29. 29

    Why am I getting this deprecated warning?! MongoDB

ホットタグ

アーカイブ