Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Dynamic Prefabs in a Multiplayer Game

Discussion in 'Connected Games' started by haroldgeronimo, Dec 3, 2018.

  1. haroldgeronimo

    haroldgeronimo

    Joined:
    Oct 12, 2017
    Posts:
    1
    Hello, sorry if my english is bad.
    So I'm currently making an RTS game. There are lots of types of characters so I decided to use scriptable objects to create different types of characters. I also created a base prefab which contains the basic components needed for a character, designed that other components will be added later on instantiation to create the different units. I inserted this base prefab on my NetworkManager as a spawnable prefab.
    I spawn units like this:
    Code (CSharp):
    1. [Command]
    2.     public void CmdSpawnObject (int spawnableObjectIndex, Vector3 position, Quaternion rotation) {
    3.        
    4.         //gets base prefab
    5.         GameObject go = NetworkManager.singleton.spawnPrefabs[UnitSpawnIndex];
    6.        
    7.         //gets desired unit scriptable object
    8.         PlayerUnit playerUnit =  unitGroup.units[spawnableObjectIndex];
    9.        
    10.         //instantiates the base prefab
    11.         go = Instantiate (go, position, rotation);
    12.        
    13.         //adds components to the base prefab using the sciptable object
    14.         playerUnit.Initialize(go);
    15.        
    16.         //assigns the unit to the player
    17.         NetworkIdentity ni = go.GetComponent<NetworkIdentity> ();
    18.         NetworkServer.SpawnWithClientAuthority (go, connectionToClient);
    19.         RpcAssignObject (ni, spawnableObjectIndex);
    20.  
    21.     }
    This creates a problem that only the server has the version of the unit that has the added components. The client on the other hand instantiates only the base prefab.

    Because of this, I tried to also add the needed components using a ClientRpc. This made it so that the unit on the client has the same components. But the when I invoke Commands on the added components this error comes up:

    upload_2018-12-3_23-55-53.png

    Thank you to anyone who will help.
     
  2. dontdiedevelop

    dontdiedevelop

    Joined:
    Sep 18, 2018
    Posts:
    8
    Code (CSharp):
    1. NetworkIdentity ni = go.GetComponent<NetworkIdentity> ();
    you're calling networkidentity component in server but clients don't know that component try this
    Code (CSharp):
    1. RpcAssignObject (go, spawnableObjectIndex);
    and in RpcAssignObject get networkidentity component with go.GetComponent<NetworkIdentity>();