# problem using OnStartServer() for unity multiplayer networked game

UPDATE: i am still scratching my head over this. The way i see it, the Awake() function is only invoked when I load it in the Editor, but if I put the prefab Loading line into OnStartServer() it just doesnt load anything, presumably because it hasnt managed to load the prefab yet ... i think it is something to do with when it switches Scenes from the offline to online in NetworkManager, i think this script and others are being dropped because the DontDestroyOnLoad command wasnt called due to Awake not being called.

Hello everyone I am making a football game which you control one player from the team. It will connect 11v11 players per server (using a "Host" acting as server and client, and other clients connecting to them)

I believe I have followed the examples and information provided by Unity to the letter, but I can't seem to get it working properly.

The problem: My code seems to work fine, provided I open the 'Host' instance from the Unity Editor, but if I make the full build version the Host it doesnt work.

Strangely all my player-objects will all spawn and act exactly as I intended, but the ball will not spawn unless the 'Host' instance is the one opened inside Unity Editor. I've made the Ball a Network Identity, NetworkTransform etc and Registered Prefab in the NetworkManager in inspector.

Here's the code I am using to spawn the ball on the server, what could be the problem as I really have no ideas, thanks:

using UnityEngine;
using UnityEngine.Networking;

public class MatchdayManager : NetworkBehaviour {
GameObject ballPrefab;

private void Awake()
{

}

public override void OnStartServer()
{
Debug.Log("on server start called");
GameObject go = Instantiate(ballPrefab);
NetworkServer.Spawn(go);
}

}

• i am still scratching my head over this. The way i see it, the Awake() function is only invoked when I load it in the Editor, but if I put the prefab Loading line into OnStartServer() it just doesnt load anything, presumably because it hasnt managed to load the prefab yet Jun 17, 2018 at 15:54

OK well I found a workaround style solution. I also came across some info here https://forum.unity.com/threads/when-awake-will-be-call-for-game-object-with-networkidentity.441678/ , which states that it is actually a known bug (for many months/years/iterations of unity!) where Awake is not called on NetworkBehaviour scripts, unless its in the Editor.

My solution is really annoying for me as it means calling gameobject.SetActive on the gameobject for my 'MatchdayManager : NetworkBehaviour' class constantly in Update() in my GameManager class (which is derives from MonoBehaviour). So I had to create an Update loop in the GameManager class and make a public variables and drag the MatchdayManager object into it, in the update loop first line is constantly setting the matchdaymanager to Active every frame (otherwise Unity is automatically setting it to inactive whilst I am in the 'offline scene' of my game).

using UnityEngine;

public class Game : MonoBehaviour {
public static Game instance;
public MatchdayManager matchdayManager;

// Use this for initialization
void Awake() {
if (instance == null)
{
instance = this;
}
else
{
return;
}

}

void Update () {
if (!matchdayManager.gameObject.activeInHierarchy)
{
matchdayManager.gameObject.SetActive(true);
}
}
}


and...

using UnityEngine;
using UnityEngine.Networking;

public class MatchdayManager : NetworkBehaviour {
public static MatchdayManager instance;

public GameObject ballPrefab;

public void Awake()
{
instance = this;

}

public override void OnStartServer()
{

Debug.Log(Time.time + " OnStartServer invoked");

GameObject go = Instantiate(ballPrefab);
NetworkServer.Spawn(go);

}

}


With this workaround my ball is now loading on all clients and the host client , whichever I use (ie. Build version or Editor version), so i was clearly right that the Awake call wasnt getting called, but now that I set it to active every frame, it is getting called from any type of Build of the game.

If anyone understands why this is happening and can tell me a better way to handle it I would be very grateful.

thanks