Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'UNet' started by Koesys, Jul 30, 2015.

  1. Koesys

    Koesys

    Joined:
    Jul 20, 2015
    Posts:
    18
    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 :

    Code (CSharp):
    1. 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 :

    Code (CSharp):
    1. [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<int> 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.
     
  2. Koesys

    Koesys

    Joined:
    Jul 20, 2015
    Posts:
    18
    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.
     
  3. gamedevassets-sean

    gamedevassets-sean

    Joined:
    Jul 26, 2013
    Posts:
    76
    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<string> or SyncList<int> 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...

    Code (CSharp):
    1. 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.
     
    seanr likes this.
  4. Koesys

    Koesys

    Joined:
    Jul 20, 2015
    Posts:
    18
    Thanks you very much Shoganator. Following your advice, I correct my code by writing :

    Code (CSharp):
    1. [SyncVar] SyncListInt myListInt = new SyncListInt();
    Now it works all fine !! :)
    Once again, thanks.
     
    gamedevassets-sean likes this.
  5. gamedevassets-sean

    gamedevassets-sean

    Joined:
    Jul 26, 2013
    Posts:
    76
    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!
     
  6. Jordii

    Jordii

    Joined:
    Sep 14, 2010
    Posts:
    135
    Bumping this topic. For future searchers, found out you actually need the = new SyncListInt(); part. Else I got all kinds of weird errors.
     
    apollov likes this.
  7. apollov

    apollov

    Joined:
    May 27, 2017
    Posts:
    1
    Another thing is that if you subclass SynclistInt and use that class as a [SyncVar] inside NetworkBehaviour:
    Code (CSharp):
    1. public class Foo : SyncListInt{}
    2. [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