Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My First Multiplayer Project - Need some help

Discussion in 'Multiplayer' started by Phanixis, Dec 18, 2015.

  1. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Ok, I have managed to get to separate copies of my project to connect through PUN using the following:

    Copy 1:
    PhotonNetwork.ConnectUsingSettings();
    PhotonNetwork.CreateRoom(roomName);

    Copy2:
    PhotonNetwork.ConnectUsingSettings();
    PhotonNetwork.JoinRoom(roomName);

    As best as I can tell they both copies have joined the same room. Now I want to actually do something with this connection. The goal is simple, I have created a list of connected players using UI Text, and I want each copy of my program to let the player enter a name that will then appear in the UI Text on all copies connected to the room.

    To achieve this end I have created a new class called PlayerInformation with the public string playerName, this class includes the method:

    //Set the player name
    public void SetPlayerName (Text setString)
    {
    playerName = setString.text;
    Debug.Log(playerName);
    }

    I have linked this method to a UI text field, and Debug.Log indicates the script is working and the UI text field is writing to the variable playerName.

    Now I can run two copies of this program, and each copy lets can use its UI text field to write to playerName within its instance of PlayerInformation. What I need to do now is to get each copy of the program to read playerName from the instance of PlayerInformation stored in the other copy of the program connected through the PUN room. How do I go about retrieving this string variable?

    Thanks for the help.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    In the UtilityScripts folder, you can find a super useful script that does connect and join a room for you, no matter if it's the first or the second client you start.

    For names of players, you could use PhotonNetwork.playerName. Set it before you get into a room or while you are in a room and it gets synced with the other players. PhotonNetwork.playerList is the list of players in the room, so you can access each player's name.

    I suggest you do the Marco Polo Tutorial, which is very basic but helpful. It might be a little outdated but once you got this done, the questions you had should be solvable for you.
     
  3. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    I suppose I could use the inherent PhotonNetwork functionality for this task, although the idea was to get a simple but practical idea of to make a class of my own design and have it interact with both players in a specific way. I ran Quill's multiplayer FPS tutorial where he managed this feat with the pre-made FPS controller's and got some idea on how to do this, but I wanted to do it with a class I wrote from scratch. Something that just read from a UI input field a dumped text to the screen is fairly simple and straightforward.

    I will have to check out this Marco Polo tutorial and see if it provides me with anything more insightful.

    Edit:

    I have attempted the tutorial. Everything went smoothly until it was time to import a monster asset for use in the tutorial, which is a rather complex prefab utilizing several scripts. As soon as I imported the asset, Unity generated a half dozen "unityengine.component does not support slicing" errors because the tutorial wasn't written for the current version of unity. I won't be able to proceed unless I can fix all the scripts attached to this prefab, and it is quite possible other additions will also cause similar problems.

    I really do not need a fully blown 3d game to illustrate these principles to me. Just something that passes some string text between clients, or something that assigns say a circle to each client (with several on the screen) that hands off control of say the circles' color to one of the clients. Once the underlying principles are clear to me, I should be able to work from there.

    Any help along these lines would be greatly appreciated.

    Thanks.
     
    Last edited: Dec 19, 2015
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,021
    I'm sorry about the hassle. It's important for us to also support the older versions of the Editor (some use them, still).
    I get the errors as well but if you allow Unity 5.3 to upgrade the assets, the errors will go away and the Monster asset works nicely again. There's a popup dialog on import that offers the upgrade.
    Also, make sure to get that asset from our page, as we have a simple, early version of it ready to use. The Monster on the Asset Store is newer but potentially also more complex.

    If you get along with a brief summary of options, please check out this summary:
    https://doc.photonengine.com/en/pun/current/tutorials/synchronization-and-state
    Maybe that better suits for your learning habits. Hope it helps.
     
  5. Rainbooow

    Rainbooow

    Joined:
    Jan 26, 2016
    Posts:
    8
    Got the same problem about the monster import (using Unity 5.3.1). And Unity was not proposing the automatic update. However, if you replace manually each "animation" with "GetComponent.<Animation>()" in CharacterControl.js (and one in myThirdPersonController.js), then after that, Unity is proposing the automatic update :).
     
    tobiass likes this.
  6. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Thanks for all the help. I found another tutorial that I like which had me create everything form scratch, basically just some cube with a custom script to control them that gave me some better understanding of the principles, but if I need to go back to the marco polo one I will. I still haven't found a good way to pass text but I kind of got sidetracked learning other aspects of unity I will need for my project.