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 Mannfred80 · Jul 12, 2016 at 08:18 AM · multiplayerprefabscharactersnetwork.instantiatenetworkplayer

Multiplayer different player prefab

Hi there I have build a multiplayer game where a player can choose his/her character he/she wants to be. If I play the game on my pc (open up two versions of the game) it is working perfectly, but when I run it on different pc's the host selection prefab is the same for everyone else (clients). I don't know what the problem.. is it to do with when the host start the game is says "client address = localhost", but on the client side it says "client address = 192.168.0.3"... Please help

Here is the code of the Network Manager

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class netmanagercustom : NetworkManager {
 
     public class MsgTypes
     {
         public const short PlayerPrefab = MsgType.Highest + 1;
         
         public class PlayerPrefabMsg : MessageBase
         {
             public short controllerID;    
             public short prefabIndex;
         }
     }
     
     public class NetworkController : NetworkManager
     {
         [SerializeField] Vector3 playerSpawnPos;
         [SerializeField] GameObject character1;
         [SerializeField] GameObject character2;
         [SerializeField] GameObject character3;
         [SerializeField] GameObject character4;
         [SerializeField] GameObject character5;
         [SerializeField] GameObject character6;
         [SerializeField] GameObject character7;
         [SerializeField] GameObject character8;
         [SerializeField] GameObject character9;
         [SerializeField] GameObject character10;
         [SerializeField] GameObject character11;
 
         GameObject chosenCharacter;
 
         public short playerPrefabIndex;
         
         public override void OnStartServer()
         {
             NetworkServer.RegisterHandler(MsgTypes.PlayerPrefab, OnResponsePrefab);
             base.OnStartServer();
         }
         
         public override void OnClientConnect(NetworkConnection conn)
         {
             client.RegisterHandler(MsgTypes.PlayerPrefab, OnRequestPrefab);
             base.OnClientConnect(conn);
         }
         
         private void OnRequestPrefab(NetworkMessage netMsg)
         {
             MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
             msg.controllerID = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>().controllerID;
             msg.prefabIndex = playerPrefabIndex;
             client.Send(MsgTypes.PlayerPrefab, msg);
         }
         
         private void OnResponsePrefab(NetworkMessage netMsg)
         {
             MsgTypes.PlayerPrefabMsg msg = netMsg.ReadMessage<MsgTypes.PlayerPrefabMsg>();  
             playerPrefab = spawnPrefabs[msg.prefabIndex];
             base.OnServerAddPlayer(netMsg.conn, msg.controllerID);
             Debug.Log(playerPrefab.name + " spawned!");
         }
         
         public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
         {
             MsgTypes.PlayerPrefabMsg msg = new MsgTypes.PlayerPrefabMsg();
             msg.controllerID = playerControllerId;
             NetworkServer.SendToClient(conn.connectionId, MsgTypes.PlayerPrefab, msg);
 
             //var player = (GameObject)GameObject.Instantiate(chosenCharacter, playerSpawnPos, Quaternion.identity);
             //NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
         }
     }
 
         [SerializeField] Vector3 playerSpawnPos;
         [SerializeField] GameObject character1;
         [SerializeField] GameObject character2;
         [SerializeField] GameObject character3;
         [SerializeField] GameObject character4;
         [SerializeField] GameObject character5;
         [SerializeField] GameObject character6;
         [SerializeField] GameObject character7;
         [SerializeField] GameObject character8;
         [SerializeField] GameObject character9;
         [SerializeField] GameObject character10;
         [SerializeField] GameObject character11;
         
         GameObject chosenCharacter;
         
         void Update()
         {
             if (PlayerPrefs.GetInt ("PlayerNumber") == 0) {
                 chosenCharacter = character1;
             }
             
             if (PlayerPrefs.GetInt ("PlayerNumber") == 1) {
                 chosenCharacter = character2;
             }
             
             if (PlayerPrefs.GetInt ("PlayerNumber") == 2) {
                 chosenCharacter = character3;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 3) {
                 chosenCharacter = character4;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 4) {
                 chosenCharacter = character5;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 5) {
                 chosenCharacter = character6;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 6) {
                 chosenCharacter = character7;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 7) {
                 chosenCharacter = character8;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 8) {
                 chosenCharacter = character9;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 9) {
                 chosenCharacter = character10;
             }
 
             if (PlayerPrefs.GetInt ("PlayerNumber") == 10) {
                 chosenCharacter = character11;
             }
         }
         public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId){
             var player = (GameObject)GameObject.Instantiate (chosenCharacter, playerSpawnPos, Quaternion.identity);
             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
         }
     }
 
Comment
Add comment · Show 2
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
avatar image Mannfred80 · Jul 12, 2016 at 07:29 AM 0
Share

Ok, I have check with a few Debug.Logs what is going on. The "PlayerPrefs.SetInt" does work and it sees the correct number over the network, but the problem is that it is not spawning the correct Character associated with the specific "PlayerPrefs" counter.

avatar image Mannfred80 · Jul 12, 2016 at 09:27 AM 0
Share

Ok i got it right, Finally... If you also have this problem, here is how to fix it.. link text

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help me please with Unity Multiplayer 1 Answer

problem using OnStartServer() for unity multiplayer networked game 0 Answers

How to reduce lag in Network Transform Componented Player in UNET? 2 Answers

Prefab not spawning on client correctly 1 Answer

How do i access a network instantiated object? 1 Answer


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