Array.ToList() Returning Error

Hello!

I have been encountering a problem while converting my Array into a List using the .ToList() method (I’m a little new to Object Oriented Programming and Networking so I apologize if my code is messy).

I have a class named “Player” and I have a list named “players” which is a list of type Player (List).

I need to send this list to all clients connected to the server, and an RPC will not let me send lists. I looked up how to do it, and I was told to convert my list to an array (using .ToArray()) and then converting it back to a list on the other end (using .ToList()). However, I receive the following error:

error CS0411: The type arguments for method `System.Linq.Enumerable.ToList(this System.Collections.Generic.IEnumerable)’ cannot be inferred from the usage. Try specifying the type arguments explicitly

This is my code:

For sending the RPC and converting the array to a list:

networkView.RPC ("updateClients", RPCMode.Others, players.ToArray ());

For receiving and setting the players list on the clients end:

[RPC] void updateClients(Array serverPlayers){
    
            players = serverPlayers.ToList ();

}

Also:

using System.Linq;

is included at the top of the script.

I have looked up this error, but have found nothing that could help me.

Does anyone know what I am doing wrong? Would it make more sense to just switch everything to an Array?

Thank you!

Never use “Array” as type. What’s the type of your original “players” variable? Are you sure it contains a type that can be send via RPC?

edit

As i said below, Unity only allows a few types as parameters for an RPC method. Arrays aren’t supportet at all with only one exception: a byte array. This allows us to use C# / .NETs serialization system to serialize any data into a byte array. This array will be transferred via RPC and deserialized on the other side. However all Unity types can’t be serialized because they don’t have the System.Serializable attribute.

I’ve just written some helper classes which allows you to serialize NetworkViewIDs and NetworkPlayer values:

NetworkSerializationHelpers

I added an example of serializing a List of “Player” values. Note that the Player class has a SerializedNetworkPlayer instead of a NetworkPlayer. This will transfer the information properly. Keep in mind that all other Unity stuff like Vector2 / 3 / 4, Quaternion, Rect, Color, … are not serializable. You would need to store them in simple fields like floats or create similar serializable wrapper structs for those classes.

If you need to transfer a lot of data / huge object trees the byte array can grow quite a bit. You might want to use the SharpZipLib library to compress the data stream. SharpZipLib is a 100% managed implementation and runs on all platforms, even web and mobile (at least Android and iOS are verified).

You can fix your error by specifying the type

players = serverPlayers.ToList<Player> ();

However this will not work. You have deeper issues to fight. Your RPC call has just sent a list of references to Players to another client. Those players do not exist on the other client.

In order for this to work you have to serialise your data down to something that can be passed across the network. Strings are often used for this. Good luck.

If you really don’t want to learn how to do all this there are some prebuilt networking solutions out there. Photon is one I’ve used in the past.