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 /
  • Help Room /
avatar image
0
Question by Febio · Jun 16, 2016 at 03:23 PM · unity 5networkinglobbyplayers

How to spawn a different game player prefab based on player choice with Network Lobby Manager?

Hi, I'm trying to to spawn a different game player prefab based on player choice with the Network Lobby Manager asset downloadable from the asset store (asset store link). To do so I'm using code on 3 different scripts:

  1. GameManager = offline game object that handles all the offline menus, included the selection menu from which the player chooses which character he wants to play with. Once the player is selected it spawns the LobbyManager prefab.

  2. LobbyPlayer = modified the code of the original adding an Update function that searches the selected game player prefab inside the GameManager.

  3. LobbyManager = modified to get the player prefab from the lobby player and spawning it instead of the default one.

I've already added all the possible game player prefabs to the spawnable prefabs in the LobbyManager inspector and given the GameManager game object to the LobbyPlayer inspector directly.

The GameManager has no network identity and simply stores the selected prefab in a public GameObject variable (chosenPC). I don't think the code of the GameManager is needed to evaluate the problem so I will omitt it unless requested.

Here's the code I added to the LobbyPlayer:

 public GameObject PlayChar = null;
 public GameObject ScreenMan; //GameManager
 void Update()
         {
             if (PlayChar == null && ScreenMan.GetComponent<manageGame>().chosenPC != null)
             {
                 PlayChar = ScreenMan.GetComponent<manageGame>().chosenPC;
                 Debug.Log(PlayChar.name);
             }
         }

And here's the code I added in the LobbyManager:

 public Dictionary<int, GameObject> currentPlayers = new Dictionary<int, GameObject>();
 public Dictionary<int, LobbyPlayer> currentLobbyPlayers = new Dictionary<int, LobbyPlayer>();
 
 public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
         {
              //original script
             GameObject obj = Instantiate(lobbyPlayerPrefab.gameObject) as GameObject;
             //original script
             LobbyPlayer newPlayer = obj.GetComponent<LobbyPlayer>(); 
             //original script
             newPlayer.ToggleJoinButton(numPlayers + 1 >= minPlayers); 
 
             //my added code
             if (!currentLobbyPlayers.ContainsKey(conn.connectionId))
             {    /*takes connectionsID into the Dictionary and the LobbyPlayer 
                 to have correspondence between the 2 and be able to distinguish which player has selected which character*/
                         currentLobbyPlayers.Add(conn.connectionId, newPlayer);
                   
             }
             .... //original code goes on
      }
 
 //this code was all added by me
 public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
         {
             LobbyPlayer newPlayer = currentLobbyPlayers[conn.connectionId];
             GameObject temp = null;
             /*I probably don't need this second dictionary but before this try the code was in a different function therefore I needed it and for simplicity I added as it was, I'm pretty sure this is not a problem thou*/
             if (!currentPlayers.ContainsKey(conn.connectionId))
             {    //takes connectionsID into the Dictionary and the chosen PC from LobbyPlayer
                 
                     temp = newPlayer.PlayChar;
                     if (temp != null)
                     {
                         currentPlayers.Add(conn.connectionId, temp);
                     }
                 
                 Debug.Log(temp.name);
             }
 
             GameObject actualPC = currentPlayers[conn.connectionId];
             Vector3 startpos = new Vector3(0.0f, 0.0f, 0.0f);
             GameObject _temp = (GameObject)GameObject.Instantiate(actualPC, startpos, Quaternion.identity);
 
             NetworkServer.AddPlayerForConnection(conn, _temp, playerControllerId);
 
             return _temp;
 }

This game uses the client/host network modality, indipendently of the fact that the unity execution is the host or the client the LobbyPlayer prefabs don't have the PlayChar variable defined in the inspector (once instantiated of course). So, to correct the scope of my question, how can I pass this information to the LobbyPlayer so that the LobbyManager is able to access it?

I've started using Unity only a couple months ago therefore I'm sorry if my code isn't very good or I've missed something simple, the fact is that I've been banging my head on this problem for 3 whole days and I've tried any approach I could come up with. Any kind of help will be greatly appreciated, thanks!

Comment
Add comment · Show 1
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 richardboegli · Feb 03, 2017 at 11:45 PM 0
Share

Did you end up with a usable solution?

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

105 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

Related Questions

Unity Lobby, player get disconnected if game has already started. 1 Answer

OnLobbyServerSceneLoadedForPlayer not called for client 1 Answer

UNet - Is there a way to get clients and their assigned gameobjects? 0 Answers

How to count score on all players from non player object 0 Answers

How do I get the true position (Vector 3) from a Network Transform Component(Unet)? 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