[Photon Networking] Passing My Player scripts 'Target' object to the Uncontrolled version of My Player


I am quite further a long in my project now, just trying to setup some multiplayer. I have pretty much everything working, that I need so far… Except… well, the targeting!

If I play on my computer, and my mobile at the same time, it works awesome. My players are both there, and they are in real time, and they are moving how they should.

The problem is with the NPC’s in the game.

If I for instance, select an NPC in my game, and then choose to attack it - all from my mobile.

My player on both screens, will go to the target, however only one will start attacking the NPC.

The movement is being tracked, but not the scripts variables - which the targets are GameObjects in this case.

Now I did some testing with other variables, and things like Strings, Booleans and Integers… Even Vectors work. But GameObjects seem not to. So at the moment, I have specific GUI and everything popping up exactly how it should and health bars and levels remain the same across all clients. But NOT FOR TARGETING TARGETS! D:

I have tried a few methods, one which seems to sometimes work, so I don’t think it’s down the correct avenue of code.

Is there any way to send GameObjects through a network AS a variable, so that the uncontrolled viewer of my player on the client of another person, can then be fed the correct -updated - information.

Any help or links are appreciated, I’ve been searching for 2 days and have only got so far.



So, I figured it out.
The code I used is as follows.

Call Method:

if (target_object.GetComponent<PhotonView> () != null) {
    	int target_id = target_object.GetComponent<PhotonView> ().viewID;
    	photonView.RPC ("networkSetTargetObject", PhotonTargets.All, target_id);

The above method triggers the RPC displayed below. It sends the viewID of the GameObject in question.

void networkSetTargetObject(int view_id) {
if (view_id != null) {
	if (photonView != null) {
		GameObject testSubject = PhotonView.Find (view_id).gameObject;
			target_object = testSubject;

You don’t need all the if statements in the networkSetTargetObject() function, I don’t think. But they have prevented a lot of errors from coming up during runtime.

What we are doing - is getting the gameObject that the passed over the network viewID is assigned to.

And that’s it, this works, all done.