Unity 3.1 Webplayer: access method failed

Hi all,

I'm running a C# script in Unity 3.1.0f4, connecting to a remote server. The web-player build run from a browser generates the following error message -

MethodAccessException: Attempt to access a private/protected method failed.

at System.Security.SecurityManager.ThrowException (System.Exception ex) [0x00000] in <filename unknown>:0 
at [Some constructor in my code]

The code looks like this:

Socket.Connect(hostname, port); //works
NetworkStream netStream = Socket.GetStream();//works
BufferedStream bStream = new BufferedStream(netStream);//seems to fail

When I, however, start the same project in the Editor, (with platform switched to web player, of course) it works without problems. It only occurs using the browser-plugin.

Even if I replace the `NetworkStream` with a `MemoryStream`, it fails when constructing the `BufferedStream`:

Socket.Connect(hostname, port); //works
NetworkStream netStream = Socket.GetStream();//works
MemoryStream test = new MemoryStream();//works as well
BufferedStream bStream = new BufferedStream(test);// also fails

As far as I could read from the documentation, both classes `MemoryStream` and `BufferedStream` are fully supported by the web player. `NetworkStream` is accessible as well, just the `close(timeout)` method is not - which I'm not using anyway.

Thanks for all hints :)

Hi,

Don’t know if you still need help. Today we also had a MethodAccessException in our logs. It only occured on a Windows Vista PC, on all PCs with Windows 7 it just worked fine.

In the end it was a Hashtable deserialization that was made in the web player. It turns out that the Hashtable deserialization constructor

Hashtable(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)

is protected, so it can’t be called during deserialization.

Our workaround was to just derive our own class from the Hashtable:

[Serializable]
public class SerializableHashtable : Hashtable
{
    public SerializableHashtable()
    {
    }

    public SerializableHashtable(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

Strange behaviour, but it works for now.