• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by any7000 · Apr 20, 2013 at 06:04 PM · networkinginstantiatemultiplayerspawn

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 $$anonymous$$t 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 be$$anonymous$$nd 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 t$$anonymous$$ngs happend and have the feeling that they are connected. Any help woul be $$anonymous$$ghly 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 () {
 
     //smoot$$anonymous$$ng 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).

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by any7000 · Jun 04, 2013 at 11:18 PM

meanw$$anonymous$$le 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 t$$anonymous$$s is of some help for someone else too.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer

MLAPI spawn player prefab 2 Answers

Network.Instatiate Problem 0 Answers

ReplacePlayerForConnection works but... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges