Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

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

Discussion in 'Multiplayer Networking' started by Koesys, Jul 30, 2015.

  1. Koesys

    Koesys

    Joined:
    Jul 20, 2015
    Posts:
    17
    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:
    17
    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. Shoganator

    Shoganator

    Joined:
    Jul 26, 2013
    Posts:
    67
    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:
    17
    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.
     
    Shoganator likes this.
  5. Shoganator

    Shoganator

    Joined:
    Jul 26, 2013
    Posts:
    67
    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:
    127
    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