Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 MadDevil · Mar 28, 2018 at 07:38 AM · networkingmultiplayernetworkmultiplayer-networking

Spawn Cube doesnt Show up on client. (Unity Networking)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 using UnityEngine.UI;
 
 public class NetworkManagerScript : NetworkDiscovery {
 
 
     //    setup a client, wait for sometime if there is no server found
     //    setup a server
     //    connect the client and server.
 
     public GameObject cube;
 
     NetworkClient myClient;
     bool isConnectedToServer;
 
     public Text msg;
 
     IEnumerator Start () {
 
         Application.runInBackground = true;
 
         SetUpClient ();
         yield return new WaitForSeconds (10f);
         if (!isConnectedToServer)
             SetUpServer ();
 
         ClientScene.RegisterPrefab (cube);
     }
 
 
     public void SetUpClient()
     {
         Debug.Log ("Starting as client");
         Initialize ();
         StartAsClient ();
     }
 
     public override void OnReceivedBroadcast(string fromAddress, string data)
     {
         if (isConnectedToServer)
             return;
 
         Debug.Log("Server Found");
         msg.text = "Server Found";
 
         string _ip = fromAddress;
         int _port = int.Parse (data);
         msg.text = "broadcast recieved. network client";
 
         myClient = new NetworkClient ();
         myClient.RegisterHandler (MsgType.Connect, OnConnected);
         myClient.Connect (_ip, _port);
 
     }
 
     void SpawnOnClient(NetworkMessage netMsg)
     {
         //ClientScene.spawnableObjects
         msg.text = "Object spawned on server.";
     }
 
 
 
     public void OnConnected(NetworkMessage netMsg)
     {
         Debug.Log ("Connected to server.");
         isConnectedToServer = true;
         msg.text = "Connected to server.";
         myClient.RegisterHandler (MsgType.ObjectSpawn, SpawnOnClient);
 //        StartAsClient ();
 //        StartCoroutine (SpawnCube());
     }
 
     public void OnClientConnected(NetworkMessage netMsg)
     {
         Debug.Log ("Client connected to server.");
         //isConnectedToServer = true;
         msg.text = "Client connected to server.";
         StartCoroutine (SpawnCube());
     }
 
     IEnumerator SpawnCube()
     {
         yield return new WaitForSeconds (2f);
         GameObject obj = GameObject.Instantiate(cube);
         NetworkServer.Spawn(obj);
         //Transform go = (Transform)Network.Instantiate (cube,Vector3.zero,Quaternion.identity,0);
         //cube.SetActive(true);
         yield return null;
         //NetworkServer.Spawn(cube);
         yield return null;
 
 
 //        while (true) {
 //            go.position = new Vector3 (Random.Range(-5.0f,5.0f), 0f, Random.Range(-5.0f,5.0f));
 //            yield return new WaitForSeconds (3f);
 //        }
     }
 
     public void SetUpServer()
     {
         int serverPort = createServer();
         if (serverPort != -1)
         {
             Debug.Log("Server created on port : " + serverPort);
             NetworkServer.RegisterHandler(MsgType.Connect, OnClientConnected);
             broadcastData = serverPort.ToString();
             Initialize ();
             StartAsServer();
             msg.text = "Server Created on port : " + serverPort;
         }
         else
         {
             Debug.Log("Failed to create Server");
             msg.text = "Failed to create server.";
         }
     }
 
     int minPort = 10000;
     int maxPort = 10010;
     int defaultPort = 10000;
 
 
     int createServer()
     {
         int serverPort = -1;
         bool serverCreated = NetworkServer.Listen(defaultPort);
 
         if (serverCreated)
         {
             serverPort = defaultPort;
         }
         else
         {
             Debug.Log("Failed to create with the default port");
             msg.text = "Failed to create with the default port";
             for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
             {
                 if (tempPort != defaultPort)
                 {
                     if (NetworkServer.Listen(tempPort))
                     {
                         serverPort = tempPort;
                         break;
                     }
 
                     if (tempPort == maxPort)
                     {
                         Debug.LogError("Failed to create server.");
                         msg.text = "Failed to create server.";
                     }
                 }
             }
         }
         return serverPort;
     }
     void GetServerStats()
     {
         Debug.Log (NetworkServer.connections.Count);
     }
 }
 



I am trying to spawn a cube by using NetworkServer.Spawn(obj) in the registercallback when the client gets connected to the server. The cube has network identity attached to it and it Instantiates on the host side but it doest show on the client side.

I am pulling my hair off trying to figure this out from last 2 days, neither google or the documentation is helping, so any kind of help will be greatly appreciated.

Thanks, Murtaza

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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

165 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can i make multiplayer game without unity multiplayer service ? 1 Answer

[Command] call is not being executed 0 Answers

How to write a multiplayer game that uses a dedicated server? 0 Answers

Unity networking tutorial? 6 Answers

Unity relay server without matchmaking 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