How to instantiate separate players across the network through a button?

Lethn
Lethn
So I've got some very simple code that by all accounts should work but PUN treats this code as if it goes across the entire network rather than just the one client, is this just because of how PhotonNetwork.Instantiate works? How would I get it to make sure that the instantiation only happens for the current client? I thought of using if (!PhotonView.IsMine ) but PUN complained a lot when I did that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreatePlayerScript : MonoBehaviour

{
	[PunRPC]
	public static GameObject localPlayer;
	public GameObject spawnAreaEmpty;
	public GameObject spawnButton;
	public float radius;

	[PunRPC]
	public void SpawnPlayer ()

	{
		spawnButton.SetActive ( false );
		PhotonNetwork.Instantiate ( "localPlayerEmpty", spawnAreaEmpty.transform.position , Quaternion.identity, 0 );
		Debug.Log ("Player Instantiated");
	}

}

Comments

  • Lethn
    Lethn
    edited January 2019
    I just wanted to say, I found the answer to the problem, it turns out it was to do with my playercontroller script and camera script being active at the time of instantiation and I found a really good video by Quill18 which explains the answer to the problem.



    Now I have a new problem, even though this code technically 'works' my camera isn't switching to the main one when I deactivate it and spawn in the player despite it existing in the scene.
    
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class CreatePlayerScript : MonoBehaviour
    
    {
    	[PunRPC]
    	public static GameObject localPlayer;
    	public GameObject spawnAreaEmpty;
    	public GameObject spawnButton;
    	public float radius;
    
    	[PunRPC]
    	public void SpawnPlayer ()
    
    	{
    		spawnButton.SetActive ( false );
    		GameObject playerGameObject = PhotonNetwork.Instantiate ( "localPlayerEmpty", spawnAreaEmpty.transform.position , Quaternion.identity, 0 );
    		playerGameObject.transform.Find("MainCamera").gameObject.SetActive ( true );
    		playerGameObject.GetComponent<MultiplayerPlayerController>().enabled = true;
    		GameObject.Find ("SpawnMenuCamera").gameObject.SetActive ( false );
    	}
    
    }
    

  • I just solved it I think, I will be doing some testing to check it works for multiple players connecting but I'm not longer getting errors, the problem was me using GameObject.Find when I should have been GetComponentInChildren().enabled = true;

    I found the answer on the comments page in an old tutorial on PUN by Quill18, Unity must have been just randomly checking for any gameobject with that name so it probably didn't like that so I need to point out specifically that I wanted just the camera that had been instantiated with my player to activate rather than just searching the whole scene for one.

    No idea of why that is but it seems to be working now, I love how it was a 2 year old comment that saved me lol.

    If your using new unity : myPlayerGO.GetComponentInChildren().enabled = true; myPlayerGO.GetComponentInChildren().enabled = true; myPlayerGO.GetComponentInChildren().enabled = true; myPlayerGO.GetComponentInChildren().enabled = true; your welcome :)