Search code examples
unity-game-enginemultiplayer

My Game object is not visible when I try and spawn in multiplayer


When I press the Lan host button when in unity multiplayer, it uses the prefab I assigned and Runner(Clone) appears in the hierarchy. But it does not appear on the screen. Then when you go into the scene view you see that there is an object but that for some reason it is not visible. What is the problem causing this and how do I fix it?


Solution

  • There can be many issues but I will give you a list of things to check.

    Is the object visible in the host / server? is the camera position / rotation can properly view the spawned object's position? Have you spawned the object using Instantiate in the same way of Single Player, or properly used the Unity Network's way of instantiating?

    This Unity Networking Tutorial talks in detail how to set up multiplayer game with a very concrete example. If you are new to Unity Networking, I recommend you start with the tutorial. The following shows how to instantiate objects in Multiplayer:

    [Command]
    void CmdFire()
    {
       // This [Command] code is run on the server!
    
       // create the bullet object locally
       var bullet = (GameObject)Instantiate(
            bulletPrefab,
            transform.position - transform.forward,
            Quaternion.identity);
    
       bullet.GetComponent<Rigidbody>().velocity = -transform.forward*4;
    
       // spawn the bullet on the clients
       NetworkServer.Spawn(bullet);
    
       // when the bullet is destroyed on the server it will automaticaly be destroyed on clients
       Destroy(bullet, 2.0f);
    }
    

    Note that the above is not enough to instantiate objects over network. There are other stuff that must be set up in order to execute the above code. Multiplayer is very different from SinglePlayer and if you are not familiar with the code above, you should definitely go to check the Unity Networking Tutorial.

    Few things to note:

    1. Method must be tagged with [Command]
    2. Method name must start with Cmd.
    3. The linked tutorial is probably the only tutorial provided by Unity. (Unity Networking is infamous for its lack of documentation.)