简体   繁体   中英

Unity Networking: can't spawn scene objects on all clients

I can't really figure out how to solve my problem. I have been looking for an answer but I couldn't find anything.

I have a button in my scene that can be pressed both by client and host. When the button is pressed, it creates a cube in the scene. The problem is that: the cube can be created only by the host and the host is the only user that can see it and manipulate it. My code is:

public class CreateCube : NetworkBehaviour {


        GameObject cubo;
        float lastCollisionTime=0;
        float collisionTime=0;

        void OnCollisionExit(Collision other) {

                collisionTime = Time.time;
                if (collisionTime - lastCollisionTime >1.5) {
                    CmdCreaCubo ();
                    lastCollisionTime = collisionTime;
                }
            }
            }
        }
        [Command]
        void CmdCreaCubo(){
            GameObject cubo=Instantiate(Resources.Load("MyPrefabs\\Oggetti\\CubeGrasp")) as GameObject;
            cubo.transform.position = new Vector3 (-5.88f, 7.51f, -19f);
            cubo.name = "CubeGrasp";
            NetworkServer.Spawn (cubo);

        }
}

Could anyone help me please? Thank you so much

Instead using simple Instantiate you should need to use Network.Instantiate

The given prefab will be instanted on all clients in the game. Synchronization is automatically set up so there is no extra work involved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM