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

Can't Spawn objects

Discussion in 'Multiplayer' started by Bigbeef, Mar 30, 2017.

  1. Bigbeef

    Bigbeef

    Joined:
    Apr 6, 2013
    Posts:
    31
    Trying to spawn a projectile so it's networked between two clients, and I thought I did everything correctly, but I get an error "NetworkServer is not Active. Cannot spawn Objects without an active server".

    This doesn't make sense to me, because I build and run the project and I click Host button. In the inspector, I run the game and click Client button on the networkmanagerGUI. They are connected and everything else seems to work fine. I can pause the Host and it pauses the client, I can move players around and see them moving on both the host and client, etc.

    I followed the instructions here:
    https://docs.unity3d.com/Manual/UNetSpawning.html

    But I still get the error.

    The 2 games are connected.
    The projectile prefab is registered in the network manager.
    The projectile prefab has a network identity (no boxes checked though).

    In my code I instantiate the object based on the prefab:

    GameObject ball = Instantiate(ballPrefab);

    I run code to set up it's variables for it's velocity and all that

    Finally I call:

    NetworkServer.Spawn(ball);


    But I get that error. Am I missing a step? Any help on this would be amazing.

    Thank you.
     
  2. Bigbeef

    Bigbeef

    Joined:
    Apr 6, 2013
    Posts:
    31
    Bump.

    Still can't figure out why I'm getting a "NetworkServer is not active" error, when trying to spawn a projectile.

    I have other networked aspects working. For example, I have a system setup where I pause all clients remotely using SyncVar attribute. If I pause the game on the Host, all my connected clients pause.

    So clearly, they're connected and there is a server running.

    So does anyone have any idea why I would still get this "NetworkServer is not active" error when trying to spawn a projectile?
     
    Last edited: Apr 2, 2017
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    No one is going to be able to help you without your code and more details. For example you haven't even said whether you're getting this error on the host, your clients, or both.

    Also make sure you're only doing this instantiation/spawning on the server.

    Lastly, I'm not sitting at a computer with Unity, but I don't recall this code ever working, as I thought Instantiate returns an Object instead of a GameObject:

    GameObject ball = Instantiate(ballPrefab);

    I've always had to write it as:

    GameObject ball = (GameObject)Instantiate(ballPrefab);

    Maybe that changed though or I'm remembering incorrectly.
     
    Last edited: Apr 4, 2017
    DarlingNavi likes this.
  4. Bigbeef

    Bigbeef

    Joined:
    Apr 6, 2013
    Posts:
    31
    Thanks for replying Joe. Ya I figured that maybe showing my code might have gotten a faster response, but you were correct. Basically, I had to ensure that the spawning code was only running on server. It even says so in the documentation, I guess I didn't catch it before. Documentation says:

    Instantiate and Spawn
    In Unity, GameObject.Instantiate creates new Unity game objects. But with the networking system, objects must also be “spawned” to be active on the network. This can only be done on the server....

    Whops!


    Also, if it helps you any, the Instantiate() function is overloaded and 2 or 3 of the overloaded function versions are templated to allow you to just call it like I did, by just passing in a GameObject for the first parameter (the func then returns a GameObject). This might be new, but it's worked like this since I started working with Unity (just a few months ago).


    The last thing I need to finish is getting my NetworkDiscovery component to work correctly with my NetworkManager component.

    Right now I can use NetworkManagerHUD and the NetworkDiscoveryHUD:

    1. I host game on 1 instance by pressing the "Host" button.
    2. I press NetworkDiscovery Instantiate button -> then click the Broadcast Server button

    3. I go to my other machine running the game on my LAN.
    4. Run the game -> press Instantiate button on my NetworkDiscoveryGUI -> press Listen button
    5. I see a button pop up displaying the IPAddress of the Host machine
    6. I click it, nothing happens, it fails to connect.

    The weird thing is

    If I run the game twice, 2 instances, on one machine, the network discovery works perfectly. Host one, broadcast, swap to other instance of the game running, click listen, see the ipaddress button appear, click it, it connects and game works perfectly.

    Any idea if I have to adjust any of my LAN settings? or if my NetworkDiscovery component attributes / NetworkManager component attributes need to be changed from the stock settings for it to work over the LAN?
     
    Last edited: Apr 6, 2017
  5. sanatan

    sanatan

    Joined:
    Nov 14, 2015
    Posts:
    10
    The lack of help on the unity forums is really disgusting. I wonder if the Unreal engine has better support.