Hello, pretty new to networking on unity so this could be a basic mistake. I checked a bit what I could have had wrong, but cannot find the problem.
My project is currently : the network manager (basic script), a game manager that has to, among other things, spawn the gameobjects at the beginning of the game.
so :
- All objects I wish to spawn have the network identity component, and are added to the networkmanager spawnable list.
- Gamemanager is server only, no need to have it on the client (?)
- here’s the code (could not find a code editor, is there one ?) :
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using System;
public class GameManager : NetworkBehaviour
{
private List colors = new List {Color.blue, Color.red, Color.green, Color.black};
public GameObject cityObject;
public GameObject districtObject;
public GameObject newHenchman;
private List players = new List ();
private List cities = new List();
public bool launchGame = false;
private bool isGameInit = false;
// Use this for initialization
void initGame ()
{
// Add a player
GameObject[ ] newPlayers = GameObject.FindGameObjectsWithTag(“player”);
int number = 0;
foreach (GameObject player in newPlayers) {
Player playerscript = player.GetComponent ();
playerscript.name = “player” + number;
playerscript.color = colors [number];
player.GetComponent ().position = new Vector3 (0, 3, 0);
number++;
players.Add (player);
}
// Add a city with a single district
GameObject city = (GameObject)Instantiate (cityObject, new Vector3(0, 0, 0), Quaternion.identity);
buildBasicCity (“city1”, city);
NetworkServer.Spawn(city);
//dummy
GameObject cityDummy =(GameObject)Instantiate(cityObject, new Vector3(0, 0, 0), Quaternion.identity);
NetworkServer.Spawn(cityDummy);
// Add 2 base henchman
foreach (GameObject player in players) {
createHenchman (city.GetComponent().stockPile.gameObject, player);
}
// Add it to the list of cities
cities.Add (city);
isGameInit = true;
}
// Update is called once per frame
void OnGUI() {
if (isGameInit == false && GUI.Button (new Rect (10, 10, 150, 100), “Launch Game”))
initGame ();
}
public void buildBasicCity(string name, GameObject cityObject){
// Create the script and set values
City cityScript = cityObject.AddComponent ();
cityScript.name = name;
cityScript.priority = new CityPriority ();
cityScript.districts = new List ();
// Create the gameObject
//Vector3 cityPosition = cityObject.transform.position;
GameObject newDistrict = (GameObject) Instantiate (districtObject, new Vector3(-2, 0.5f, -2), Quaternion.identity);
// Add the district component and add this district to the city
newDistrict.name = “firstDistrict”;
DistrictFactory.createBaseDistrict (newDistrict);
cityScript.addDistrict (newDistrict.GetComponent());
NetworkServer.Spawn(newDistrict);
// Repeat for a second district
newDistrict = (GameObject) Instantiate (districtObject, new Vector3(2, 0.5f, -2), Quaternion.identity);
DistrictFactory.createBaseDistrict (newDistrict);
newDistrict.name = “secondDistrict”;
cityScript.addDistrict (newDistrict.GetComponent());
NetworkServer.Spawn(newDistrict);
// Add a stockpile
newDistrict = (GameObject) Instantiate(districtObject, new Vector3(2, 0.5f, 2), Quaternion.identity);
newDistrict.AddComponent ();
newDistrict.name = “stockPile”;
cityScript.stockPile = newDistrict.GetComponent();
NetworkServer.Spawn(newDistrict);
// Add a recruit spot
newDistrict = (GameObject) Instantiate(districtObject, new Vector3(-2, 0.5f, 2), Quaternion.identity);
newDistrict.AddComponent ();
newDistrict.name = “recruitPlace”;
cityScript.recruitSpot = newDistrict.GetComponent();
NetworkServer.Spawn(newDistrict);
// Shuffle the priority order
cityScript.priority.updateList(GameManager.getPlayersForCity(cityScript));
}
public void createHenchman (GameObject district, GameObject player)
{
GameObject newObject = (GameObject)Instantiate (newHenchman, new Vector3(district.transform.position.x, district.transform.position.y + 1, district.transform.position.z), Quaternion.identity);
newObject.GetComponent().material.color = player.GetComponent ().color;
Henchman script = newObject.GetComponent ();
script.setLocation(district);
player.GetComponent().henchmans.Add (newObject);
NetworkServer.Spawn (newObject);
Debug.Log (“Henchman created”);
}
So I create my objects, add and modify scripts given what I want, and then spawn them on the network. Problem is, the host see the objects, and they appear in the unity list of objects, but the client does not. Would you have an idea ?