Search Unity

[Solved] error CS0117: 'UnsafeUtility' does not contain a definition for 'MemSet'

Discussion in 'Entity Component System' started by Skyblade, May 29, 2019.

  1. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    I have the following packages in my project:
    upload_2019-5-29_21-57-53.png

    As you can see, there are no compilation errors in my code.
    Why do I see error CS0117: 'UnsafeUtility' does not contain a definition for 'MemSet' error?
    Where does it come from?
     
    Last edited: May 30, 2019
  2. Harry-Wells

    Harry-Wells

    Joined:
    Oct 13, 2013
    Posts:
    73
    I found that downgrading the Collections package to 0.0.9-preview.19 killed the error. It's something to do with 0.0.9-preview.20
    (The package is normally hidden as it's a preview package)
     
    Last edited: May 29, 2019
    Skyblade likes this.
  3. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    That seems to do the trick. Thanks!
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Code (CSharp):
    1. #if  UNITY_2019_3_OR_NEWER
    2.             UnsafeUtility.MemSet(destination, value, count);
    3. #else
    Was curious why I had no issue, and that's because it'll only show up on 2019.3a

    Side note, UnsafeUtility.MemSet will be very handy
     
    deus0, psuong, Skyblade and 1 other person like this.
  5. nemoman1991

    nemoman1991

    Joined:
    May 2, 2015
    Posts:
    1
    sorry,can you tell me how to downgrading the Collections package to 0.0.9-preview.19
     
  6. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    126
    You can bump down the version in Packages/manifest.json.

    upload_2019-7-10_10-3-17.png
     
  7. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    What sort of things do you tend to use it for out of interest?
     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Mostly resetting arrays (to -1)

    An example is the NativeMultiHashMap where the .Clear method has to iterate all the buckets and set the values to -1. Instead you could use UnsafeUtility.MemSet

    I've had uses for it in algorithms where I want the default value of an array to be -1 representing the cell has not be visited.
     
  9. chubshobe

    chubshobe

    Joined:
    Jun 20, 2015
    Posts:
    52
    The Collections package is not in the Package Manager, even when viewing preview packages. Packages/manifest.json doesn't contain the Collections package either. Even after restarting Unity more than it was probably necessary, the Collections package didn't show up. The weird thing is that it's in the PackageCache folder. Deleting it, and then launching Unity still didn't work.It was a very poor decision to install the Burst compiler.
     
  10. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    The Entities package depends on collections, burst, etc so all those packages will be installed.

    The original issue in this thread was already fixed so what exactly is the issue you are you having?
     
  11. chubshobe

    chubshobe

    Joined:
    Jun 20, 2015
    Posts:
    52
    The exact same error message, started appearing after installing the Burst compiler. Removing the Burst compiler package from the package manager doesn't really work, it does some stuff but the package is still there.
     
  12. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Show us your manifest. Also what version of Unity.
     
  13. chubshobe

    chubshobe

    Joined:
    Jun 20, 2015
    Posts:
    52
    Unity 2019.3.0a3, pretty much everything has been said so far. Switched the API Compatibility Level to .NET 4.x from .NET Standard 2.0 before installing the Burst package, as it was recommended. That error appeared right after installing.
     
  14. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    That's the problem the issue is fixed in newer versions of 2019.3 (latest version is a8, can't remember what version fixed it.)

    Upgrade your version of Unity.
     
  15. chubshobe

    chubshobe

    Joined:
    Jun 20, 2015
    Posts:
    52
    Updated to a8, using the same project, installed the latest version of the Burst package. The error is gone, thanks.
     
  16. danielz4fun

    danielz4fun

    Joined:
    Sep 15, 2019
    Posts:
    1
    In my case the mistake was similar but it was another, if anyone can help I would be very grateful, I need to fix it urgently.
     

    Attached Files:

  17. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    226
    @tertle Could you provide an example of this usage? Not sure how to use the byte "value" parameter to set elements in an int array to -1.

    Edit:
    Thanks already as I saw your post here:
    https://forum.unity.com/threads/nativelist-remove-and-removeat-methods.778859/#post-5183357
     
    Last edited: Jun 9, 2022
  18. Harry-Wells

    Harry-Wells

    Joined:
    Oct 13, 2013
    Posts:
    73
    If you consider the in-memory representation of a C# int, that’s 4 bytes in an architecture-dependent order. The byte order actually doesn’t matter in this case: the int -1 is just 4 bytes where each is 255 unsigned or -1 signed.
    Because of this, with a pointer to an int buffer, you can just pass 255 as the value to set and 4 * intArrayLength for the size.
     
    apkdev and chadfranklin47 like this.
  19. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    226
    Thanks for the explanation. I was hoping to be able to set all ints in an int array to positive 1... but I guess MemSet() isn't designed for that so I'll need to stick with MemCpyReplicate().
     
    Last edited: Jun 9, 2022