Photon - Find Instantiated GameObject

Options
Hello all,

I'm working on a multiplayer game and I've ran into an issue.
The game is basically a space shooter when 2 players control a single space ship (one is the pilot the other one is a gunner).

I use PhotonNetwork.Instantiate in order to create the spaceship and then spawn into them each of the players.
But in my current version each player gets his own spaceship ("Fighter").

Fighter - is the spaceship and the spots array holds spawn spot for each player role.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class NetworkManager : Photon.MonoBehaviour {

	int whoAmI;
	public OVRCameraRig ovrCam;
	public Camera cam;//test cam
	spawnSpot[] spots;
	int groupId;
	GameObject Fighter;
	Camera[] displayCams;

	void getGroupId(){
		int num = PhotonNetwork.player.ID;
		//Debug.Log ("player.ID "+num);
		if (num % 2 == 0) {
			groupId = (num / 2) - 1;
			//return;
		} else {
			groupId = num / 2;
			//return;
		}
		Debug.Log (groupId);
	}

	public void ConnectAsPliot(){

		Debug.Log("ConnectAsPliot to PhotonServer");
		PhotonNetwork.ConnectUsingSettings("alpha");
		whoAmI = 0; //Pilot
	}

	public void ConnectAsGunner(){
		Debug.Log("ConnectAsGunner to PhotonServer");
		PhotonNetwork.ConnectUsingSettings("alpha");
		whoAmI = 1; //Gunner
	}

	void OnGUI(){
		GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString());
		GUILayout.Label ("Number of players in room "+PhotonNetwork.countOfPlayers.ToString());
		GUILayout.Label ("I R NUMBER " + PhotonNetwork.player.ID);
		if (!(Fighter == null)) {
			GUILayout.Label ("Number of players in room "+Fighter.transform.position.ToString());		
		}
	}

	void OnJoinedLobby(){
		Debug.Log("OnJoinedLobby");
		PhotonNetwork.JoinRandomRoom ();
	}
	void OnPhotonRandomJoinFailed(){
		PhotonNetwork.CreateRoom (null); // need to change room name to something with value
		Debug.Log("OnPhotonJoinRoomFailed");
	}
	void OnCreateRoom(){
		Debug.Log ("OnCreateRoom");
	}
	void OnJoinedRoom(){
		Debug.Log ("OnJoinedRoom");
		getGroupId ();
		Debug.Log("GroupId  "+groupId);

		Fighter = PhotonNetwork.Instantiate ("fighter", Vector3.zero, Quaternion.identity,/*groupId*/0);
		spots = Fighter.GetComponentsInChildren<spawnSpot>();
		spawn ();
	}

	void spawn(){

		displayCams = Fighter.GetComponentsInChildren<Camera> ();
		displayCams [0].enabled = false;
		displayCams [1].enabled = false;
		displayCams [2].enabled = false;
		//Fighter.GetComponents<d
		if (spots == null) {
			Debug.LogError ("unable spawn to a spot, spots = null");
		}
		Debug.Log ("My role(0 pilot 1 gunner) is: "+whoAmI);
		spawnSpot mySpot = spots [whoAmI];

		cam.transform.position = mySpot.transform.position;
		cam.transform.rotation = mySpot.transform.rotation;
		
		cam.transform.position = new Vector3 (ovrCam.transform.position.x, 5.319f, ovrCam.transform.position.z);//need to change the spawn height and then to remove this line
		Fighter.GetComponent<networkFighter> ().myCam = cam;
		Fighter.GetComponent<networkFighter> ().displayCams = displayCams;
		cam.GetComponent<CameraFollow> ().SetTarget (mySpot.transform);

		/*oculus section
		ovrCam.transform.position = mySpot.transform.position;
		ovrCam.transform.rotation = mySpot.transform.rotation;

		ovrCam.transform.position = new Vector3 (ovrCam.transform.position.x, 5.319f, ovrCam.transform.position.z);//need to change the spawn height and then to remove this line
		Fighter.GetComponent<networkFighter> ().myCam = ovrCam;
		ovrCam.GetComponent<CameraFollow> ().SetTarget (mySpot.transform);
*/
	}

}

I've edited the 'void OnJoinedRoom()' method as follows:

    void OnJoinedRoom(){
        Debug.Log ("OnJoinedRoom");
        getGroupId ();
        Debug.Log("GroupId  "+groupId);
 
        int MYID = PhotonNetwork.player.ID;
        Debug.Log ("MY ID IS" + MYID);
 
        if (MYID == 1) {
                        Fighter = PhotonNetwork.Instantiate ("fighter", Vector3.zero, Quaternion.identity,/*groupId*/0);
                        spots = Fighter.GetComponentsInChildren<spawnSpot> ();
                        spawn ();
           
        } else {
            ********************************************************************
            spots = Fighter.GetComponentsInChildren<spawnSpot>();
            spawn ();
        }
    }
 
 
}

But the 'else' clause is missing the Instantiated fighter game object.
I need to find a way to get the existing and already Instantiated fighter game object.
How can I do that?

Thanks.

Comments

  • vadim
    Options
    Hi,

    Never use concrete value of player.ID for logic. It's not guaranteed that player with id = 1 will join the room ever. Checking if client is master is more appropriate approach for scene initialization. But you still need to handle OnMasterClientSwitched in case if room creator (initial master) disconnected before it managed to initialize scene.

    In your case, maybe easier just create fighter by 'pilot' player.
    You can update NetworkManager's reference to fighter in fighter's OnPhotonInstantiate handler.
  • Thanks, How do I update the Fighter reference to the Gunner player once he connects?
  • vadim
    Options
    Implement OnPhotonInstantiate in scripts of fighter object and update reference in this method:
    NetworkManagerInstance.Fighter = this.gameObject