Unity3D Unsafe code requires the `unsafe' command line option to be specified

22,551

Solution 1

Go to your project properties page and check under Build the checkbox Allow unsafe code. This should solve your problem.

Solution 2

Here is what worked for me with Unity3D 5.3:
- If in your Unity Build Setting->Player Setting->Other Settings, "API Compatibility Level" is ".Net 2.0", create a file under your Unity Asset folder called gmcs.rsp, add this following line to the file:
-unsafe
Close Unity and open it again.
- If the "API Compatibility Level" is ".Net 2.0 Subset", the above file name should be: smcs.rsp.

See the picture bellow to find the location of ""API Compatibility Level" setting in Unity.
enter image description here

Solution 3

The answer is given here: http://answers.unity3d.com/questions/804103/how-to-enable-unsafe-and-use-pointers.html

You need to add a file "smcs.rsp" (or "gmcs.rsp") in your "Assets" directory, which contains the line:

-unsafe
Share:
22,551
or azran
Author by

or azran

Updated on March 31, 2020

Comments

  • or azran
    or azran about 4 years

    i am using Unity3D 4.3 and calling a DLL that i created. when trying to call the only function it have which is this:

    void GetModelReferences(int &nVertices, float * vertices, int &nTriangles, int * triangles, float * normals, float * uvCoordinates);
    

    unity gives me an error:

    Unsafe code requires the `unsafe' command line option to be specified
    

    so in my MonoDevelop i opened : Project->Assembly-Csharp options and turned on the unsafe mode.

    it reduces part of the errors but this last one wont go away

    Unsafe code requires the `unsafe' command line option to be specified
    

    what shoud i do?

  • or azran
    or azran almost 10 years
    the unsafe code is in the c# script that attached to an object in unity, what then?
  • CodeTherapist
    CodeTherapist almost 10 years
    Is the unity project a C# project? If so, there is also a property page for this. If not, I simply not know, how unity is executing the script.
  • or azran
    or azran almost 10 years
    ok, i know i need to add the option\flag but i dont know how to get there
  • or azran
    or azran almost 10 years
    i am running from a pc
  • cowlinator
    cowlinator over 9 years
    Miloš Selečéni, which version of Unity is this for? Mine has a Frameworks folder, but no MonoCompiler.framework.
  • Cross_
    Cross_ over 8 years
    In MonoDevelop: menu Project/Assembly-CSharp Options then Build/General/ Allow unsafe
  • MrPaulch
    MrPaulch over 7 years
    This was the only usefull answer for me. Since I can not use .NET 2.0 Subset and my Unity Project Settings do not have the option to allow unsafe code.
  • c0ffeeartc
    c0ffeeartc over 4 years
    when targeting the .NET 3.5 Equivalent (deprecated) scripting runtime version, mcs is used with mcs.rsp, and when targeting the .NET 4.x Eqivalent scripting runtime version compiler, csc is used with csc.rsp.