- Home /
Network.Instantiate prefab with added parts
Hello, I'm having some problems with Network.Instantiate.
A player is able to customize their items and those items have gfx that can be loaded via children components with SpriteRenderers such as this.
GameObject Player = Object.Instantiate(NetworkManager.Instance.SpawnPlayer) as GameObject;
GameObject test = new GameObject();
test.transform.parent = Player.transform;
test.transform.name = "Hat";
test.AddComponent<SpriteRenderer>();
test.GetComponent<SpriteRenderer>().sprite = Resources.Load("Hat1") as Sprite;
Now this works perfectly fine for 1-player scenarios, problem is this is meant to be a multiplayer game. If I load add the extra items in this manner, they aren't shown on the other clients when I Network.Instantiate the prefab (obviously, since the items were added after).
I can't Network.Instantiate this new updated Player GameObject because Network.Instantiate wants a prefab, but if I save the prefab using PrefabUtility (only works in Editor), the prefab is permanently replaced.
How would I go about having a base-player prefab (just a body), adding items to it programmatically and then instantiating that new object across the network? Thanks in advance.
Edit: Thought of a potential solutions, but wondering if there is a simpler implementation.
Once each player finishes their Network.Instantiate, get the list of items the player has and use RPCs to attach those items to that player in every client's list of Player GameObjects.
Answer by Cynikal · Apr 05, 2014 at 10:52 PM
The way I would go about it is...
Since Instantiate doesn't actually send the item itself, it just sends the data. The object must be present on all systems to work properly.
Easiest solution would be to make various prefabs of the same object, add your effects and whatnot... then call each one specifically.
Considering it's a base player with over 100 various items for 5 different item slots (each one having a distinct visual effect on the player sprite), making prefabs of every single combination is simply not feasible. I went about it by Network.Instantiate-ing a base-prefab and then calling RPCs with the NetworkPlayer id and the item list to find the right GameObject in each client and add the sprites locally in every client. Not the simplest solution but it's significantly simpler than making all of the prefabs and it seems to work.