UNetWeaver error: Exception :System.InvalidCastException: Cannot cast from source type to destinatio

Hello. Sorry for english mistakes :

I had a code working quite properly with unity 5.1.1f1
I just install the patch 5.1.2p1

Then, I have that error :

UNetWeaver error: SyncVar [System.Int32[] tableaux::TableauChoixOP] cannot be an array. Use a SyncList instead.

The problem is obviously in a line where I had written that :

[SyncVar] public int[] TableauChoixOP = new int[256];

So I am googling the problem, as I am quite a begginer in Game Development : I find a few answer, but really not much. So I try to change my line with :

(… ok. I was just about to copy past the line from monodevelop, I copy, I paste, … that appears :
ℼ佄呃偙⁅呈䱍倠䉕䥌⁃ⴢ⼯㍗⽃䐯䑔䠠䵔⁌⸴‰牔湡楳楴湯污⼯久㸢䠼䵔㹌䈼䑏㹙䘼乏⁔慦散㴠✠潍…etc
Wtf. Another bug.) Anyway, the code I talk about is :

public static SyncList TableauChoixOP;

I adapt a few thing, and now, I have that error :

“UNetWeaver error: Exception :System.InvalidCastException: Cannot cast from source type to destination type.”

And that, even google can’t find ANY clue which could lead to a little hope part of a single solution (yeah, even google). There is also a lot of text in that error, and nothing seems relative to my code.

Please, could you tell me what happened and how does the SyncList works ? Thank you very much.

I notice than everything that I am copy paste from MonoDevelop on this forum become japanese. (or else, I am not sure).
It paste fine on other places.

The problem appears to be that UNET internally tries to do some code weaving using mono cecil to figure out how to treat your list of integers, but cannot cast the type to one it wants when it does this. If I declare a public field of type SyncList or SyncList I get the same issue.

I’m not sure what the difference is, but you may want to try and instead use the SyncListInt type - it would appear that Unity have wrapped this up to do what you need it to do…

SyncListInt myListInt = new SyncListInt();

This type appears to ensure that values are synchronised across clients, and also includes a callback that you can subscribe to, so you can run callback methods on clients when values are added or removed from the list.

Hope that helps.

1 Like

Thanks you very much Shoganator. Following your advice, I correct my code by writing :

[SyncVar] SyncListInt myListInt = new SyncListInt();

Now it works all fine !! :slight_smile:
Once again, thanks.

1 Like

No problem - glad it helped. For your information, I don’t believe you need to have the [SyncVar] attribute on a SyncList - you should be able to just leave it off, and your SyncList should still work!

Bumping this topic. For future searchers, found out you actually need the = new SyncListInt(); part. Else I got all kinds of weird errors.

1 Like

Another thing is that if you subclass SynclistInt and use that class as a [SyncVar] inside NetworkBehaviour:

public class Foo : SyncListInt{}
[SyncVar] Foo foo = new Foo();

the build will fail with something cryptic like

Leaving you no chance to understand what you’ve done wrong.
So be careful