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!

Ive never used ToList (ive never used LINQ)
Just use the array as the parameter in construction of the list
myList = new List(myArray)

On the receiving end change

[RPC] void updateClients(Array serverPlayers){

to

[RPC] void updateClients(Player[] serverPlayers){

and I believe that should fix it.

(The error you’re getting is basically saying it doesn’t know what type of class the “Array” contains, since “Array” doesn’t have a type to it, and could be holding anything. You should usually never use “Array” like that as a parameter, you will almost always want to have the type of the array, such as “Player[ ]” or “int[ ]”.)