- Home /
Spawning Dynamically created GameObjects on a network
Hello, I have been trying to spawn my Tile prefab on a server so that any clients that connect will have the same tile spawned in the same position. The problem, however, is that when the Tile is spawned for the clients, the sprite property on the SpriteRenderer component is set to "None (Sprite)" for all clients except the host even though it was set to a value before being spawned.
Is it possible to spawn a dynamically created GameObject so that it synchronizes all of its component data when spawned on clients?
This is the code (C#) for spawning the Tile on the server:
void InstantiateTileOnServer(string baseName, string resourceName, Vector3 coord)
{
GameObject tile = (GameObject)Instantiate(tilePrefab, coord, Quaternion.identity);
tile.transform.Find("Base").GetComponent<SpriteRenderer>().sprite = tileEntries.tileDict[baseName].sprite;
if(resourceName != null)
{
GameObject resource = new GameObject("Resource");
resource.AddComponent<SpriteRenderer>();
resource.GetComponent<SpriteRenderer>().sprite = tileEntries.tileDict[resourceName].sprite;
resource.GetComponent<SpriteRenderer>().sortingOrder = 1;
resource.transform.SetParent(tile.transform);
resource.transform.localPosition = Vector3.zero;
}
NetworkServer.Spawn(tile);
}
The "Tile" prefab has a Transform component, PolygonCollider2D component, and a NetworkIdentity component (No options selected). The "Tile" prefab also has a child GameObject called "Base" which has a Transform and a SpriteRenderer component.
My Unity version is 5.2.1f1