How do I enable unsafe code?

I’ve tried going to Assembly-CSharp->options->Build->General and checking the allow unsafe code checkbox. I tried Assembly-CSharp->options->Compiler and in the additional options text box, I tried adding both -unsafe and /unsafe. But I still get the error stating I need to allow unsafe code when I try building my app. Is there some other way I can enable unsafe code?

You can, but it will only make it run in the editor, still wont work when making a build, as far as i know there are no way of making unsafe code run in a build. : /

Make a text file named smcs.rsp (for .Net 2.0 Subset) or gmcs.rsp (for .Net 2.0) with a single line inside:

-unsafe

and save in somewhere in your Assets folder. That’s it.

Hello,

I am having the same problem.

I tried placing those files named smcs.rsp`and gmcs.rsp` (containing the line `-unsafe`) in the Assets folder but I keep getting the compilation errors about the unsafe`thing.

I am using Unity 4.2.2f1 (Pro version activated for a 30 days trial) and Visual Studio Express 2010 to compile my C++ dll library.

My DLL library compiles fine and is being placed into my unity project folder/Plugins.

I`ve written a small unity script that I have attached to a Cube object.

Here is my c|<# code:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Joystick : MonoBehaviour {

public struct SJoyXY
{
float x, y;
// bool bInitialised;
};

public unsafe SJoyXY pos;

[DllImport (“JoystickDLL”)]
private static extern SJoyXY* GetJoystickPosition();

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

pos = GetJoystickPosition();
Debug.Log( "Joystick position is : " + SJoyXY.x );

}
}

I get the < following errors when compiling:

c:\Users\Public\Documents\Unity Projects\JoystickDLL\Assets\Standard Assets\Scripts\Joystick.cs(23,23): Error CS0227: Unsafe code may only appear if compiling with /unsafe (CS0227) (Assembly-CSharp-firstpass)

c:\Users\Public\Documents\Unity Projects\JoystickDLL\Assets\Standard Assets\Scripts\Joystick.cs(25,25): Error CS0214: Pointers and fixed size buffers may only be used in an unsafe context (CS0214) (Assembly-CSharp-firstpass)

I`ve read alot of posts on the net, but non works with my system.

I guess theren is something I do wrong but I can`t figure it out and I already spent too much time on it.

Compiling where? Both Visual Studio and MonoDevelop still need checked “Allow unsafe code” in project settings to successfully compile unsafe code. But since Unity compiles everything itself, it needs only smcs.rsp or gmcs.rsp.

public unsafe SJoyXY pos; // There's nothing unsafe here, you may remove this keyword.

[DllImport ("JoystickDLL")]
private static extern SJoyXY* GetJoystickPosition();

...

private void Update()
{
	pos = GetJoystickPosition();
	Debug.Log("Joystick position is : " + SJoyXY.x); // Accessing a static field that doesn't exist
}

Does GetJoystickPosition really returns a pointer to SJoyXY? Inside Update it looks like it returns the whole structure. If it does return the whole structure, try this:

using System.Runtime.InteropServices;
using UnityEngine;

public class Joystick : MonoBehaviour
{
	public struct SJoyXY
	{
		public float x;
		public float y;
	};

	public SJoyXY pos;

	[DllImport("JoystickDLL")]
	private static extern SJoyXY GetJoystickPosition();

	private void Update()
	{
		pos = GetJoystickPosition();
		Debug.Log("Joystick position is : " + pos.x);
	}
}

Thank you very much Alexzzz; I got it !

My problem was that I had created smcs.rsp with notepad and it had named it “smcs.rsp.txt”.

I also was missing the unsafe checkbox, that was searching in Solution propertiesinstead of ``Project propertie" if “first pass c|…”.

For the poin<ters, you are definitely right. In my depair I had been changing my code here and there; my C++ function returns a pointer and I should use pointers everywhere.

I`m soo happy !!!

Thanks again !

It seems Unity will get a new user :slight_smile:

I’m failing to get this to work in Pro. I’ve added both .rsp files (also tried just one), have checked “allow unsafe code” in all three of the files in the solution explorer. No luck though. What am I missing?

Nevermind, it started working as soon as I commented out the unsafe code, saved it, then played and stopped it in the editor which triggered VS2012 to reload the solution. I then uncommented the unsafe code, saved it, and it worked.

You can run unsafe code from within a .NET DLL in the editor or builds without changing any configuration settings.

Good to know. Thanks.