referring to an object instantiated by the client

Options
In my game the player that enters the server instantiated a boat, this boat and connected to him is his owner with the name he put at the beginning of the game, but when I want to teleport to my boat I go to the masterclient boat because the two boat they have the same name
script attach to the instantiated player to teleport to the boat
  • boat = GameObject.FindGameObjectWithTag ("boat");
    io.transform.position = new Vector3 (boat.position.x, boat.transform.position.y, boat.transform.position.z);

this is the script I wrote to get connected and instantiate


public class NetworkManager : Photon.MonoBehaviour {

public static NetworkManager netManager;
GameObject spawnpoint;
GameObject spawnpointboat;
public Text respawnText;
bool isDead = false;
float spawnDelay = 3f;
float timeNeeded;
public GameObject menuPanel;
public GameObject gamePanel;
public InputField nameField;

GameObject[] lootspawn;

void OnGUI()
{
GUI.Label (new Rect (10, 10, 100, 100), PhotonNetwork.connectionStateDetailed.ToString ());

}

// Use this for initialization
void Start () {
netManager = this;
timeNeeded = spawnDelay;

PhotonNetwork.autoJoinLobby = true;
lootspawn = GameObject.FindGameObjectsWithTag("coinspawn");

spawnpoint = GameObject.Find ("spawnpoint");
spawnpointboat = GameObject.Find ("spawnpointboat");
}

void Update()
{
if(isDead)
{
respawnText.text = "Respawn" + Mathf.Round(timeNeeded);
timeNeeded -= Time.deltaTime;
if(timeNeeded < 0.5f)
{

isDead = false;
timeNeeded = spawnDelay;
respawnText.gameObject.SetActive (false);
RespawnPlayer ();
}
}
}

void OnJoinedLobby()
{
PhotonNetwork.CreateRoom (null);
//PhotonNetwork.JoinRandomRoom();
}
//Ho fallito la funziona joinradomroom...ovvero no stanza trovata
void OnPhotonJoinRoomFailed()//onPhotonRandomJoinFailed()
{

}

void OnJoinedRoom()
{
// PhotonNetwork.Instantiate("caracter", spawnpoint.transform.position, spawnpoint.transform.rotation, 0);
PlayerIsDead();
menuPanel.SetActive (false);
gamePanel.SetActive (true);
if(PhotonNetwork.isMasterClient)
{

foreach(GameObject obj in lootspawn)
{

obj.GetComponent ().enabled = true;
}
}
// PhotonNetwork.Instantiate("boat",spawnpointboat.transform.position, spawnpointboat.transform.rotation, 0);
}
//Evocato e inizio gioco e dopo il tempo di spawn
void RespawnPlayer()

{
PhotonNetwork.Instantiate("caracter", spawnpoint.transform.position, spawnpoint.transform.rotation, 0);

PhotonNetwork.Instantiate("boat",spawnpointboat.transform.position, spawnpointboat.transform.rotation, 0);
}
//chiamat da player damage ogni volta che muore
public void PlayerIsDead()
{
isDead = true;
respawnText.gameObject.SetActive (true);

}
public void onConnectBtnPressed()
{
if(nameField.text != "")
{
PhotonNetwork.player.NickName = nameField.text;
if(!PhotonNetwork.connected)
{
PhotonNetwork.ConnectUsingSettings ("islandv0.1");
}
else
{
OnJoinedLobby();
}
}
}
}


Comments

  • Hi @hillmatic,

    GameObject.FindGameObjectWithTag ("boat");
    only finds one active object with that tag. If you have multiple objects with the same tag, only one active found will be returned. There is another function which returns all objects with a certain tag. This is GameObject.FindGameObjectsWithTag("tag");.
  • hillmatic
    Options
    thanks I solved :) instantiating the boat from the player and setting player.parent = boat.transform,but I do not know why now the model of the player that is parent to the boat object is invisible,but even if synchronized online... I have already started from photon network without knowing anything about c # maybe I'm running into something too big xD something I learn from this forum something from the tutorial....my brain advises me maybe the player already having a photoview becoming a parent to the boat with another photon view becomes invisible?
  • hillmatic
    Options
    i found https://forum.unity.com/threads/solved-how-to-set-transform-parent-via-rpc.31336/
    1. string GetMountPath()
    2. {
    3. string str = "";
    4. Transform cur = transform.parent;
    5. while( cur.parent != null )
    6. {
    7. str = cur.name + "/" + str;
    8. cur = cur.parent;
    9. }
    10. return str.Trim( "/".ToCharArray() );
    11. }

    12. [RPC]
    13. void RPCLinkToParent( string mountPath, NetworkViewID rootID, Vector3 lPos, Vector3 lEuler )
    14. {
    15. Transform rootPoint = NetworkView.Find( rootID ).transform;
    16. //Debug.Log( "Got SyncMount for " + mountPath + " on object " + rootPoint.gameObject.name );
    17. StartCoroutine( DoMount( mountPath, rootPoint, lPos, lEuler ) );
    18. }

    19. IEnumerator DoMount( string mountPath, Transform rootPoint, Vector3 lPos, Vector3 lEuler )
    20. {
    21. Transform mountPoint = rootPoint.transform.Find( mountPath );
    22. while( mountPoint == null )
    23. {
    24. //Debug.Log( "Mount point didn't exist yet" );
    25. yield return new WaitForSeconds( 0.5f );
    26. mountPoint = rootPoint.transform.Find( mountPath );
    27. }

    28. //Debug.Log( "Mounting object " + gameObject.name + " to position " + lPos );
    29. transform.parent = mountPoint;
    30. transform.localEulerAngles = lEuler;
    31. transform.localPosition = lPos;
    32. }

    33. void SyncMount()
    34. {
    35. if( networkView.isMine )
    36. {
    37. //Debug.Log( "Sending SyncMount for " + gameObject.name, gameObject );
    38. string mountPath = GetMountPath();
    39. NetworkViewID vID = transform.root.networkView.viewID;
    40. //Debug.Log( "Root path is " + mountPath + ", root ID is " + vID );
    41. networkView.RPC( "RPCLinkToParent", RPCMode.OthersBuffered, mountPath, vID, transform.localPosition, transform.localEulerAngles );
    42. }
    43. }