Author Topic: Not able to obtain unique playerID  (Read 2637 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Not able to obtain unique playerID
« on: October 23, 2016, 08:36:52 AM »
Below is my code for creating a player (using TNet 3).

Im have managed to rename the instantiated prefab at the end.

My problem is during runtime the instantiated object has the same name for each player that joins the channel. ( see "go.name = "player" + TNManager.playerID;" in my code below) <<--which is intended to give each instantiated object a unique name.

Can I request some guidance into why this isnt working?

  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. namespace TNet
  6. {
  7.         /// <summary>
  8.         /// Instantiate the specified prefab at the game object's position.
  9.         /// </summary>
  10.  
  11.         public class TNAutoCreatePlayer : TNBehaviour
  12.         {
  13.                 public static Manager manager_instance;
  14.  
  15.                 /// <summary>
  16.                 /// ID of the channel where the prefab should be created. '0' means "last used channel".
  17.                 /// </summary>
  18.  
  19.                 public int channelID = 0;
  20.  
  21.                 /// <summary>
  22.                 /// Prefabs to instantiate.
  23.                 /// </summary>
  24.  
  25.                 string playerprefabPath;
  26.                 string charactertype;
  27.  
  28.  
  29.                 /// <summary>
  30.                 /// Whether the instantiated object will remain in the game when the player that created it leaves.
  31.                 /// Set this to 'false' for the player's avatar.
  32.                 /// </summary>
  33.  
  34.                 public bool persistent = false;
  35.  
  36.                 void Awake (){
  37.                         charactertype = PlayerPrefs.GetString ("charactertype");
  38.                         manager_instance = GameObject.FindGameObjectWithTag ("manager").GetComponent<Manager>();
  39.                 }
  40.  
  41.                 IEnumerator Start ()
  42.                 {
  43.                         while (TNManager.isJoiningChannel) yield return null;
  44.  
  45.  
  46.                         if (charactertype == "1")
  47.                                 playerprefabPath = "petmaster_world";
  48.                         if (charactertype == "2")
  49.                                 playerprefabPath = "champion_world";
  50.                         if (charactertype == "3")
  51.                                 playerprefabPath = "mystic_world";
  52.  
  53.                         if (channelID < 1) channelID = TNManager.lastChannelID;
  54.                         TNManager.Instantiate(channelID, "CreatePlayer", playerprefabPath, persistent, transform.position, transform.rotation);
  55.  
  56.                         Destroy(gameObject);
  57.                 }
  58.  
  59.                 [RCC]
  60.                 static GameObject CreatePlayer (GameObject prefab, Vector3 pos, Quaternion rot)
  61.                 {
  62.                         // Instantiate the prefab
  63.                         GameObject go = prefab.Instantiate();
  64.  
  65.                         // Set the position and rotation based on the passed values
  66.                         Transform t = go.transform;
  67.                         t.position = pos;
  68.                         t.rotation = rot;
  69.                         go.name = "player" + TNManager.playerID; // The name of the gameobject is the same for each player that joins
  70.                         return go;
  71.  
  72.                 }
  73.  
  74.  
  75.  
  76.  
  77.         }
  78. }
  79.  

Rexima

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 78
    • View Profile
Re: Not able to obtain unique playerID
« Reply #1 on: October 23, 2016, 12:32:54 PM »
  1.                 [RCC]
  2.                 static GameObject CreatePlayer (GameObject prefab, Vector3 pos, Quaternion rot)
  3.                 {
  4.                         // Instantiate the prefab
  5.                         GameObject go = prefab.Instantiate();
  6.  
  7.                         // Set the position and rotation based on the passed values
  8.                         Transform t = go.transform;
  9.                         t.position = pos;
  10.                         t.rotation = rot;
  11.                         go.name = "player" + TNManager.playerID; // The name of the gameobject is the same for each player that joins
  12.                         return go;
  13.  
  14.                 }
  15.  
  16.  

If im correct, this part will get called on all players. And when someone's joins the game, my game calls this function to create the other player and it will use my "TNManager.playerID".
I suggest you, if you have an class on your Player Gameobject, for example called "Player.cs" to put in the Start() this code:
  1. if (tno.isMine)
  2. {
  3.      gameObject.name = TNManager.playerName + "_" + tno.ownerID;
  4. }else{
  5.      gameObject.name = TNManager.GetPlayer(tno.ownerID).name + "_" + tno.ownerID;
  6. }
  7.  

Thats, how i do it. (Don't forget, to set the Playername before joining)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Not able to obtain unique playerID
« Reply #2 on: October 23, 2016, 09:11:54 PM »

  1. if (tno.isMine)
  2. {
  3.      gameObject.name = TNManager.playerName + "_" + tno.ownerID;
  4. }else{
  5.      gameObject.name = TNManager.GetPlayer(tno.ownerID).name + "_" + tno.ownerID;
  6. }
  7.  


Thanks! I tried this code and it worked. The name of the gameobjects are no longer duplicated , but are unique.

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Not able to obtain unique playerID
« Reply #3 on: October 23, 2016, 10:55:55 PM »
tno.uid works as well. ownerID is just the uid of the player that owns that object, so if a player owns more than one object you'll have duplicates. tno.uid is guaranteed unique and is assigned by the server.