Search Unity

Adding NetworkIdentity at run time

Discussion in 'Multiplayer' started by eheine, Jun 11, 2015.

  1. eheine

    eheine

    Joined:
    Jun 9, 2015
    Posts:
    5
    I'm working on a game that has a mix of single player and multiplayer scenarios with different prefabs players can choose as their avatar in each. Because the prefabs need to work in single player situations, I can't setup networking components on the prefabs themselves and I need the same prefab to work in both the single player and multiplayer scenarios.

    What I'm currently trying to do is use a wrapper prefab with a NetworkIdentity for the players in the multiplayer scenarios that will instantiate the avatar prefab that player selected to use and set it up to work across the network. I've got the avatar prefabs instantiating everywhere correctly, but need to put a NetworkIdentity component on it so I can give it a NetworkTransform component. So my question is how do I add and setup a NetworkIdentity on a GameObject at run time so that I can use NetworkBehaviours on it? Is this possible or is there some other way I should go about this? Any help would be greatly appreciated, thanks!
     
    macagu likes this.
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    Why have different prefabs? Would it work to make your game data-drive and set up a single player prefab with the needed components and data? That way, you can have a NetworkIdentity and NetworkTransform already added, and not rely multiple prefabs which will probably share some components/data.
     
  3. eheine

    eheine

    Joined:
    Jun 9, 2015
    Posts:
    5
    I don't think I'll be able to do it that way. As ridiculous as it sounds, the prefabs might be completely different types of things, sometimes vehicles, sometimes guys running around. And some of the systems used by these prefabs (like the weapons' bullets) are going to have the same issue of needing dynamic NetworkIdentities.
     
  4. eheine

    eheine

    Joined:
    Jun 9, 2015
    Posts:
    5
    Alternatively, is there some way to clone/use the information from the NetworkIdentity I do have from the player prefab that instantiates the avatar prefabs?