Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnStartServer does not get invoked

Discussion in 'Multiplayer' started by jens12, Jun 21, 2015.

  1. jens12

    jens12

    Joined:
    Jun 4, 2015
    Posts:
    5
    Hi,
    I'd like to set up a simple multiplayer environment. At server start several prefabs need to be instantiated and spawned which is why i created a game object, attached a network identity and put a script in it:

    public class Spawner : NetworkBehaviour {
    ...
    public override void OnStartServer()
    {
    // create and spawn
    }
    }

    Strangely the OnStartServer() never gets called. Could anyone shed some light on this please?
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    You using the network manager hud?
     
  3. jens12

    jens12

    Joined:
    Jun 4, 2015
    Posts:
    5
  4. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    253
    I had this too. Turns out you need to use the "override" keyword (as in "public override void OnStartServer()"), unlike with Start, Update, etc.
     
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I think they did have the override keyword - at least in the code snippet they posted they have it anyway.
     
  6. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    Im having this issue too used the line with and without overide no effect, just dosent get called..
     
  7. the_game_maker

    the_game_maker

    Joined:
    Nov 15, 2014
    Posts:
    65
    Don't you have to have NetworkConnection conn (or whatever you want to call it) inside the onStartServer() brackets
     
  8. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    If in Monodev you start to type public overide and select from the list OnStartServer it will make the function for you, it dosent put anything in the brackets, and in the unity docs says that too.. its not a big deal as i use the Start() insted on the online level and works just the same..
     
  9. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Wouldn't that cause problems if a player had a slow connection and then logic which depends on the connection having already been completed happens? Or do you mean it's like a lobby or something that establishes the connection before that scene loads with that script running it's start method?
     
  10. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    The way ive done it, on the online scene, ive placed a start() that has [Server] above it, so that it only applies to the server, this gets called when the server starts to load that level, im guessing the higher up the list that object is the first its going to be loaded, so its as close to server start as you can get i guess.

    you would but the script obiously on the level that you would expect to load when you want to see if the server has started, so this could be on the lobby scene or whichever scene the server will load at start..

    edit apparently start still gets called on client when you put [Server] obove it.. so just do this..

    Code (CSharp):
    1. void Start ()
    2. {
    3. if(!isServer){return;}
    4.  
    5. // Do server code
    6. }
    7.  
     
    Last edited: Oct 28, 2015
  11. Oskar_Kasprzak

    Oskar_Kasprzak

    Joined:
    Mar 26, 2016
    Posts:
    61
    Hello.

    Please don't ban me or give me golden shovel, but this is excatly my problem and solution provided by @LeopardX doesn't work for me.

    So in my game I have simple boolean attached to every trigger. When player clicks on it, it sets to true and instantiate an object. The problem is that when I stop the server and start it again, the value of bool is still set to true (default is false) in my code.

    Can anyone take a look, please?

    Code (csharp):
    1.  
    2. public class TriggerController2 : NetworkBehaviour {
    3.     // Use this for initialization
    4.     public bool isMarked;
    5.     void Start()
    6.     {
    7.         if (!isServer) { return; }
    8.  
    9.         isMarked = false;
    10.     }
    I also tried using OnStartServer, but no success.
     
  12. hottabych

    hottabych

    Joined:
    Apr 18, 2015
    Posts:
    107
    Check this: https://docs.unity3d.com/Manual/UNetSpawning.html -- GameObject creation flow
    OnStartServer is called after NetworkServer.Spawn() for the spawned object server instance! It's not intended for creating and spawning objects. What's the answer why you encountered problems.
     
    Last edited: Jun 28, 2018