- Home /
Networking: Object is instantiated twice instead of once
Hi! In my networking game, two players (from one prefab) are spawned at two specific points (SpawnPoint 1 and 2) when the server is initialized and a client is connected. Both players are able to hit the GUI-Button "Start" and create a ball to play with. The ball is created randomly from a list of three sphere-prefabs. The ball-script attached to the three sphere-prefabs should do the following: destroy the sphere-clone when it passes behind a player and create a new ball, so the game continues automatically. I am having the following problems: 1. Instead of only one clone two clones are spawned when the sphere-object is destroyed, e.g. the clones multiply themself over time 2. I specified that the Server-Player is spawned at SpawnPoint 1 and the Client-Player at SpawnPoint 2. But when the Client connects the Server-Player is swaped from SpawnPoint 1 to SpawnPoint 2
I am very confused why these two things happend and have the feeling that they are connected. Any help woul be highly appreciated!
I attach the following codes
Srcipt (ConnectionGUI.js) for setting up the network game, including the instantiat-functions:
//Variables for spawing prefabs
var playerPrefab : GameObject;
var sphereRange : GameObject[];
var spawnpoint1 : Transform;
var spawnpoint2 : Transform;
//Variables for GUI
var textures : Texture[];
function OnGUI(){
if(Network.peerType == NetworkPeerType.Disconnected){
if (GUI.Button (new Rect(10,90,100,30), "Connect")){
Debug.Log("button pressed");
Debug.Log(remoteIP + " " + remotePort);
Network.useNat = useNAT;
Network.Connect(remoteIP, remotePort);
}
//start server
if (GUI.Button (new Rect(10,130,100,30), "Start Server")){
Network.useNat = useNAT;
Network.InitializeServer(32, listenPort);
}
remoteIP = GUI.TextField(new Rect(120,90,100,20),remoteIP);
remotePort = parseInt(GUI.TextField(new Rect(230,90,40,20), remotePort.ToString()));
}
else{
ipaddress = Network.player.ipAddress;
port = Network.player.port.ToString();
GUI.Label(new Rect(140,100,250,40), "IP Address: " + ipaddress + ":" + port);
if (GUI.Button (new Rect(10,90,100,30), "Disconnect")){
Network.Disconnect(200);
}
if (GUI.Button (new Rect(870, 90, 50, 50), "play")){
for (var go : GameObject in FindObjectsOfType(GameObject))
go.SendMessage("spawnSphere", SendMessageOptions.DontRequireReceiver);
}
}
}
function spawnSphere(){
var mySphere : GameObject = sphereRange[Random.Range(0, sphereRange.length)];
Network.Instantiate(mySphere, mySphere.transform.position, mySphere.transform.rotation, 0);
}
//function on Client when Client is connected to server
function OnConnectedToServer(){
Debug.Log("Client connected");
Network.Instantiate(playerPrefab, spawnpoint2.transform.position, spawnpoint2.transform.rotation, 0);
}
//function when Server is initalized
function OnServerInitialized(){
Debug.Log("Server initialized");
Network.Instantiate(playerPrefab, spawnpoint1.transform.position, spawnpoint1.transform.rotation, 0);
Script (BallCont.js) attached to all sphere-prefabs (moving, adding to score, destroy and calling the instantiate function for creating a new sphere)
#pragma strict
var cSpeedC:float = 10.0;
var sFactorC:float = 10.0;
function Start () {
rigidbody.AddForce(0,0,Random.Range(-5.0,5.0));
}
function Update () {
//smoothing of ball movement
var cvelC = rigidbody.velocity;
var tvelC = cvelC.normalized * cSpeedC;
rigidbody.velocity = Vector3.Lerp(cvelC, tvelC, Time.deltaTime * sFactorC);
//check the right bounds (goal for player 1), add score and destroy
if(transform.position.z > 8){
var p1C = GameObject.Find("MainGameObject");
p1C.GetComponent(PointsGUI).scPlayer1++;
print("Player 1 spawn");
var IntA = GameObject.Find("Camera");
IntA.GetComponent(ConnectionGUI).spawnSphere();
Destroy (gameObject);
}
//check the left bounds (goal for player 2), add score and destroy
if(transform.position.z < -5){
var p2C = GameObject.Find("MainGameObject");
p2C.GetComponent(PointsGUI).scPlayer2++;
print("Player 2 spawn");
var IntB = GameObject.Find("Camera");
IntB.GetComponent(ConnectionGUI).spawnSphere();
Destroy (gameObject);
}
}
Sofar I tested the game only in local host and on one computer (two instances).
Answer by any7000 · Jun 04, 2013 at 11:18 PM
meanwhile I was able to solve the problem by adding the following code into the ConnectionGUI.js and always spawning the object from there:
function spawnSphere(){
if (!stopSpawn){
if (networkView.isMine){
yield WaitForSeconds (3);
var nPuck = Network.Instantiate(puck, puck.transform.position, puck.transform.rotation,0);
}
}
}
function spawnPlayer(){
var client : int = System.Int32.Parse(Network.player.ToString());
//Debug.Log("Client " + client);
if (client == 1){
Network.Instantiate(playerPatient1, spawnpoint1.transform.position, spawnpoint1.transform.rotation, 0);
}
if (client == 2){
Network.Instantiate(playerHealthy2, spawnpoint2.transform.position, spawnpoint2.transform.rotation, 0);
}
}
Maybe this is of some help for someone else too.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Players not moving properly - Network Multiplayer 1 Answer
Photon Networking Instantiating Problem 0 Answers
Unity Photon syncing objects 0 Answers
I dont want "(clone)" but how to change? 2 Answers