Photon - Find Instantiated GameObject

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);

        //check number of connected players - if only 1 is connected (me) - wait until another player connects
        //then Instantiate Fighter - do only for 2 players.
        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.

You could add the prefab to a list/array when you instantiate it. Whenever you need it you can find it on the list and do stuff with it. If you don’t need it anymore you can remove it from the list. It maybe sounds easier than it is but you came this far already haha. Try to think what you can do with a couple of variables that you use in a smart way, they allow you to save things so make good use of that. You can nitpick about efficiency later :slight_smile: