Search Unity

Object Sync problem

Discussion in 'Multiplayer' started by gungnir, Sep 9, 2010.

  1. gungnir

    gungnir

    Joined:
    Nov 27, 2009
    Posts:
    16
    Hi all,

    I'm working on my first networking game and i've ran into an issue.

    The following is the problem:

    I have an online game with two players. When both players are spawned i'm also spawning a TextMesh intended to float above their heads with the players name. Each player has a number e.g player 1 and player 2

    To get this to sync up i'm trying to use an RPC call, something like this:

    Code (csharp):
    1.  
    2. private var mTextMesh : TextMesh = null;
    3.  
    4. //network instantiate of the textmesh.
    5. //Something like mTextMesh = Network.Inst....etc
    6.  
    7. //RPC is called.
    8.  
    9. @RPC
    10. function SetText(str : String)
    11. {
    12.   mTextMesh.text = str;
    13. }
    14.  
    This throws an error the instant player 2 connects to the server. It gives an nullreferenceerror on mTextMesh which makes sense. The question however is, how to work around this? It's not possible to pass a TextMesh as a parameter to an RPC.

    I hope someone can help me out here :)
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    not sure why you assign a 'null' value to your textmesh, it's not needed.
    also why are you instantiating your textmesh sepperated ? you could add it to your player prefab.
    sure you can send player1's name to player2 via rpc and have that player2 set the name to player1's textmesh. you will need to use gameObject.Find() and networkView.isMine but this will only work for 2 players, when more join you will have problems.
    on this page is a network tutorial where you can find how to do it with more users if needed :
    http://www.m2h.nl/unity/
     
  3. gungnir

    gungnir

    Joined:
    Nov 27, 2009
    Posts:
    16
    I'm assigning "null" because it's a personal preference.

    I definately want this to scale to more than two player so i need something that works in all cases.

    The tutorial you are pointing me to is the one i've been using to learn about networking. It would be helpful if you could tell me the part i should look for the solution to my problem because right now i still don't have a clue.
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    it should be example 3 or 4, i don't remember which one. It's the one with the scoreboard. You can use that as a base and edit it.