Search Unity

Problem using OnStartServer() for multiplayer networked game (only works if the 'Host' is in Editor)

Discussion in 'Editor & General Support' started by megabrobro, Jun 17, 2018.

  1. megabrobro

    megabrobro

    Joined:
    Jul 8, 2017
    Posts:
    109
    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:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class MatchdayManager : NetworkBehaviour {
    5. GameObject ballPrefab;  
    6.  
    7. private void Awake()
    8. {
    9.     DontDestroyOnLoad(this);
    10.  
    11.     ballPrefab = Resources.Load<GameObject>("Prefabs/Ball");
    12. }
    13.  
    14. public override void OnStartServer()
    15. {
    16.     Debug.Log("on server start called");
    17.     GameObject go = Instantiate(ballPrefab);
    18.     NetworkServer.Spawn(go);
    19. }
    20.  
    21.  
    22. }