Photon Instantiating and Searching Problems

Options
Hi guys, so iv received a wee bug and I cant figure out whats wrong with it... It works, fine in the editor and when only one person is on the server. So when a player joins the server their canvas panel is initiated and is then that is parented to the canvas and then a certain button is initiated to the canvas panel. However, when the second player joins, the panel and canvas are instigated but they can't seem to find the canvas or canvas panel(in the buttons case) and I can't figure for the life of me why?
Any thanks would be greatly appreciated. Here is my code :

if (photonView.isMine)
{
GameObject uiGo = PhotonNetwork.Instantiate(playerUiPrefab.name, new Vector3(this.transform.position.x,
this.transform.position.y, 0), Quaternion.identity, 0) as GameObject;
playerJoyStick = GameObject.FindGameObjectWithTag("PlayerJoyStick").GetComponentInChildren();
if(playerJoyStick == null)
{
print("IM NULL");
}
uiGo.GetComponent().SetParent(GameObject.Find("Canvas").GetComponent(), false);
}

that is the code for instantiating the canvas panel and then parenting it the canvas game object. And here is the code for the buttons:


if(photonView.isMine)
{
alarmButton = PhotonNetwork.Instantiate(alarmButtonPrefab.name, new Vector3(this.transform.position.x + 50,
this.transform.position.y, 0), Quaternion.identity, 0) as GameObject;

Button alarmBut = alarmButton.GetComponent();
alarmBut.onClick.AddListener(() => { DeactivateAlarms(); });
}

private void Update()
{
if(photonView.isMine)
{
alarmButton.GetComponent().SetParent(GameObject.FindGameObjectWithTag("PlayerControls").GetComponent(), false);
}...

Best Answer

  • airbagnr1
    airbagnr1
    Answer ✓
    Options
    Hi, managed to solved it my self. Was a very stupid error - to fix it i simply made seperate canvases for the players!:)

Answers

  • M4TT
    M4TT
    edited February 2018
    Options
    Hi,
    There is a simple way to instantiate Ui element as Child of a canvas:
    public GameObject uiElement;
    newUi =  Instantiate(uiElement, new Vector3(0,0,0) , Quaternion.identity) as GameObject;
    newUi.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas"), false);
    Also you should check the anchors of your uiElement which reside in the canvas.
  • airbagnr1
    Options
    Hi Matt, thanks for the help but that still never worked - when the second player joins the game it can't seem to find the canvas still...

    I think there is an issue with If(photonView.isMine), am I correct in saying that if there are multiple scripts attached to a character but one of those scripts instantiate an object then that objects owner is still going to be that character?

    Cant seem to copy and paste an image in here but: the "BreakDoorButton" and "PlayerContorls" where instainted by the build version and they can be seen in the build and used but are not parented in the canvas when i check in the editor? Additionally, the "PlayerControls" in the canvas what instainted by the editor when connected to the server but the button didn't seem to instainte? Not really sure what is going on...?

  • airbagnr1
    airbagnr1
    Answer ✓
    Options
    Hi, managed to solved it my self. Was a very stupid error - to fix it i simply made seperate canvases for the players!:)