How to enable unsafe and use pointers

I am trying to use some pointers in my code. And i get this error in monodevelop:

Parse error: unsafe code requires the ‘unsafe’ command line option to be specified

When you're creating the file "smcs.rsp" do you create a new C# file?

3 Answers

3

I strongly recommend to not use pointers and unsafe code at all. Keep in mind that some platforms don’t support unsafe code for safety such as the webplayer for example.

If you really need an unsafe context, you have to create a “smcs.rsp” file in your asset folder and add the line

-unsafe

to that file. This should add this parameter to the c# compiler options when compiling your project.

Again, using unsafe doesn’t give you much advantages, it just opens up for a huge variety of errors, mistakes and unexpected behaviour. Unless you know exactly what you do and you use pointers sparsely you shouldn’t go for an unsafe context.

I am probably doing something wrong, but i tried twice and when i create "smcs.rsp" file in my Assests folder and put that one line nothing changes. What am i missing?

Restarted unity and everything is working right now, thanks!

Make sure this file is at the top level of Assets (i.e. Assets/smcs.rsp)

smcs.rsp should be used with .Net API Compatibility Level 2.0 Subset (Build Settings) When using the full .Net 2.0 compatibility level, the file has to be named gmcs.rsp instead

This line: when targeting MS compiler, csc is used with csc.rsp, etc. at https://docs.unity3d.com/Manual/PlatformDependentCompilation.html may be important to notice... I had to add that file too, so code could be exported to Visual Studio.

New answer

(tested in 2020.2)

You can go to Project Settings → Player and at the bottom under “Script Compliation” there is a checkbox for “Allow ‘unsafe’ Code”

In BuildSetting ,you should changes to the “Api Compatibility Level” to" .NET 2.0 Subset" yet.