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

6

7

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?

Cabal answered 1/7, 2014 at 14:30 Comment(1)
Do you have more than one project? May be that you need to check that setting on more than one project.Kauslick
Q
14

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

Quito answered 1/7, 2014 at 14:33 Comment(4)
the unsafe code is in the c# script that attached to an object in unity, what then?Cabal
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.Quito
ok, i know i need to add the option\flag but i dont know how to get thereCabal
In MonoDevelop: menu Project/Assembly-CSharp Options then Build/General/ Allow unsafeCrinkly
S
8

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

Scutate answered 12/5, 2016 at 20:54 Comment(0)
U
3

The answer is given here: https://mcmap.net/q/11034/unity-how-to-enable-unsafe-and-use-pointers

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

-unsafe
Undulate answered 6/11, 2015 at 16:21 Comment(2)
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.Kym
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.Foiled
D
0

Have you tried this ?

look at the fifth comment

Firstly show the contents of Unity.App and drill down to Contents/Frameworks/MonoCompiler.framework.

Open the file called compile_any.pl and in the section "# Run the csharp compiler" change @Base_Args options as below:

my @base_args=($mono_path, $mcs_path, '-debug', '-unsafe', '-target:library', '-nowarn:0169', '-out:'.$output, );

Decontaminate answered 1/7, 2014 at 19:14 Comment(2)
i am running from a pcCabal
Miloš Selečéni, which version of Unity is this for? Mine has a Frameworks folder, but no MonoCompiler.framework.Streaky
S
0

You can change the settings through the MonoDevelop: go to the solution files tree -> Solution -> your expand your solution -> Assempely-CSharp -> Right Click -> options -> Build -> General -> Language options -> enable "Allow 'unsafe' code"

Simulation answered 6/11, 2018 at 7:4 Comment(0)
P
0

If you're in a unity project, open Project Settings>Other Settings>Allow Unsafe Code

Polymorphonuclear answered 30/3, 2020 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.