- Home /
Problem storing GameObject reference in script - PUN
Hello!
I'm currently networking with the newest version of PUN, and I'm quickly running into trouble finding a way to access the player's attached scripts.
Some background - The game is a 2D card game. When each player joins, I instantiate their deck, junk pile, hand interface, etc. through PUN using PhotonNetwork.Instantiate.
However, I need to spawn the battlefield after both players join. I then need to assign references from the spots in the battlefield where cards can be played to public variables on both players.
Note: The battlefield consists of three smaller fields that I'm calling Realms.
This is where the problems begin. My NetworkController script can't seem to store a reference to Player1, even though I've tried both static and non-static variables. So when the field is made, neither Player1 nor Player2 receive the variable assignments.
I've tried GameObject.Find but, even though I set the name of each object after it is instantiated, the Player still does not recieve the assignment.
Here's the important part of the script.
GameObject re1 = PhotonNetwork.Instantiate ("Realm1", Vector3.zero, Quaternion.identity, 0);
GameObject re2 = PhotonNetwork.Instantiate ("Realm2", Vector3.zero, Quaternion.identity, 0);
GameObject re3 = PhotonNetwork.Instantiate ("Realm3", Vector3.zero, Quaternion.identity, 0);
AddToRealms (re1, re2, re3); //Function that adds items to List.
//The items are then randomized and put in predetermined transforms.
//In the randomizing code, re1 - re3 are turned into realm1 - realm3.
realm1.name = "Realm1";
realm2.name = "Realm2";
realm3.name = "Realm3";
GameObject p1 = GameObject.Find ("Player1");
GameObject p2 = GameObject.Find ("Player2");
p1.GetComponent<PhotonView>().RPC ("TheSCode", PhotonTargets.AllBuffered);
p2.GetComponent<PhotonView>().RPC ("TheSCode", PhotonTargets.AllBuffered);
In another script:
[PunRPC]
public void TheSCode (){
if (playerInt == 1) {
GameObject q1 = GameObject.Find ("Realm1/S1");
s1 = q1;
GameObject q2 = GameObject.Find ("Realm1/S2");
s2 = q2;
GameObject q3 = GameObject.Find ("Realm1/S3");
s3 = q3;
GameObject q4 = GameObject.Find ("Realm2/S1");
s12 = q4;
GameObject q5 = GameObject.Find ("Realm2/S2");
s22 = q5;
GameObject q6 = GameObject.Find ("Realm2/S3");
s32 = q6;
GameObject q7 = GameObject.Find ("Realm3/S1");
s13 = q7;
GameObject q8 = GameObject.Find ("Realm3/S2");
s23 = q8;
GameObject q9 = GameObject.Find ("Realm3/S3");
s33 = q9;
}else if (playerInt == 2) {
GameObject q1 = GameObject.Find ("Realm1/2S1");
s1 = q1;
GameObject q2 = GameObject.Find ("Realm1/2S2");
s2 = q2;
GameObject q3 = GameObject.Find ("Realm1/2S3");
s3 = q3;
GameObject q4 = GameObject.Find ("Realm2/2S1");
s12 = q4;
GameObject q5 = GameObject.Find ("Realm2/2S2");
s22 = q5;
GameObject q6 = GameObject.Find ("Realm2/2S3");
s32 = q6;
GameObject q7 = GameObject.Find ("Realm3/2S1");
s13 = q7;
GameObject q8 = GameObject.Find ("Realm3/2S2");
s23 = q8;
GameObject q9 = GameObject.Find ("Realm3/2S3");
s33 = q9;
}
}
s1 - s33 are what I need to set. I'm incredibly confused, as everything I try fails when it comes to resolving this. Any help would be greatly appreciated!
Your answer
Follow this Question
Related Questions
Changing a variable in another script over the network 1 Answer
Send a Pause RPC on OnApplicationPause 1 Answer
RPC or Synchronization ? 1 Answer
Multiplayer Moving Bullet 1 Answer