Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character Select on Network...

Discussion in 'General Discussion' started by Ozzyel-PT, Oct 25, 2014.

  1. Ozzyel-PT

    Ozzyel-PT

    Joined:
    Jul 27, 2013
    Posts:
    24
    Hello..
    This is actually a logic problem I can't solve.. I'm making a multiplayer game that when a Player joins he got the character selection screen.. there are 4 characters / buttons, I want that when a player pick a character it wont be available for other players (max 4 players).

    Code (CSharp):
    1.  
    2. if (!Network.isClient && !Network.isServer)
    3. {
    4. //Create / Join Server code when not connected.....
    5. }else{
    6. //Character Buttons for server and clients....
    7. for (int p = 0; p < 4; p++)
    8.                 {
    9.                   // Draw Buttons for available characters
    10.                     if (charSelect != p)
    11.                     {
    12.                         if (GUI.Button(new Rect((Screen.width / 2) - 260 + (p * 50), 230, 50, 40), p.ToString("0")))
    13.                         {
    14.                             networkView.RPC("CharacterSelect", RPCMode.All, p);
    15.                             charSelect= p;
    16.                         }
    17.                     }
    18.                 }
    19. }
    20.  
    21. [RPC]
    22. void CharacterSelect(int b)
    23. {
    24. //Don't know how to manage this
    25. }
    I hope this was clear.. any help is appreciated
    Thanks.
     
  2. LeWyRaWr

    LeWyRaWr

    Joined:
    Jul 6, 2012
    Posts:
    12
    You can do this by using classes. In the example below (untested, by the way), each character has it's own class, with it's ID, name, and selected boolean, for example.

    When you select the character, it sends an RPC to everyone else that that particular player is now selected. You could even expand on that and set the players ID and name also.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharacterSelect : MonoBehaviour {
    5.  
    6.     public PlayerCharacter myCharacter;
    7.     public PlayerCharacter[] playerCharacters;
    8.  
    9.     void Update () {
    10.  
    11.         if (!Network.isClient && !Network.isServer)
    12.         {
    13.             //Create / Join Server code when not connected.....
    14.         }
    15.         else
    16.         {
    17.             //Character Buttons for server and clients....
    18.             for (int p = 0; p < playerCharacters.Length; p++)
    19.             {
    20.                 // Draw Buttons for available characters
    21.                 if (!playerCharacters[p].selected)
    22.                 {
    23.                     if (GUI.Button(new Rect((Screen.width / 2) - 260 + (p * 50), 230, 50, 40), p.ToString("0")))
    24.                     {
    25.                         networkView.RPC("CharacterSelect", RPCMode.All, p);
    26.                         playerCharacters[p] = myCharacter;
    27.                         playerCharacters[p].selected = true;
    28.                     }
    29.                 }
    30.             }
    31.         }
    32.     }
    33.  
    34.     [RPC]
    35.     void CharacterSelect(int b)
    36.     {
    37.         playerCharacters[b].selected = true;
    38.     }
    39. }
    40.  
    41. public class PlayerCharacter
    42. {
    43.     public int playerID = -1;
    44.     public string name = "Player";
    45.     public bool selected = false;
    46. }
     
  3. Ozzyel-PT

    Ozzyel-PT

    Joined:
    Jul 27, 2013
    Posts:
    24
    Thanks for replying..
    It didn't work but I have the idea, got errors on line 21:

    NullReferenceException: Object reference not set to an instance of an object

    Looks like I have to assingn a size for this array or something.
    Anyway this method is just for select once.. my idea is to toggle the selection since the server is created and waiting for players.. like if I choose char 2..
    2 = true and 1,3, 4 = false.. so I can toggle anytime betwen these 4 buttons as long as there are no players.
    Then a players join we could have:
    Code (CSharp):
    1.  
    2. Player1                  Player2                 Player3             Global
    3. [0] = false             [0] = false                [0] = true          [0] true
    4. [1]=true                [1] = false                [1] = false         [1]  true
    5. [2]=false               [2] = true                 [2] = false         [2] true
    6. [3]= false              [3] = false                [3] = false          [3] false
    So the last player in joining will have one option [3]..
    Hope not get you more confused =]
    Thanks.