Punching Player(s) Through Photon

Hi there,

I’m working on an open-world project and I need to accomplish the Fighting script which gives me lots of problem. Basically, I have getting-hit animation, fighting and health script.

I made a BoxCollider in front of the player and attach the fighting script into it. I wanted the other player to react by playing the “gettingHit” animation and then subtract 25 to his current hp. The “punching” animation works but either the “gettingHit” animation or -25 HP works.

(1) Getting-hit animation takes one parameter which is “isHit”.

(2) Health script has a public method which is addEnergy(float, float) and mostly used for subtracting.

(3) Here is fighting script:

**

using UnityEngine;
using System.Collections;
public class Punching : Photon.MonoBehaviour {
	private Animator anim;
	private Collider otherPlayer;
	
	void Start()
	{
		anim = transform.root.GetComponent<Animator>();
	}
	void OnTriggerEnter(Collider hit)
	{
		if(anim.GetBool ("isPunching") == true)
		{
			if(hit.transform.root.tag == "Player")
			{
				hit.GetComponent<PhotonView>().networkView.RPC ("DealDamage", PhotonTargets.AllBufferedViaServer);
			}
		}
	}
	[RPC]
	void DealDamage()
	{
		GetComponent<Energy>().addEnergy(-25,-25);
		GetComponent<Animator>().SetBool ("isHit", true);
		Invoke("cancelDamage", 0.1f);
	}
	void cancelDamage()
	{
		GetComponent<Animator>().SetBool ("isHit", true);
	}
}

**

And I usually get this error, it seems a bug so I ignore it.


NullReferenceException: Object reference not set to an instance of an object
NetworkCharacter.OnPhotonSerializeView (.PhotonStream stream, .PhotonMessageInfo info) (at Assets/Script/Network/NetworkCharacter.cs:52)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
PhotonView.ExecuteComponentOnSerialize (UnityEngine.Component component, .PhotonStream stream, .PhotonMessageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:536)
PhotonView.DeserializeComponent (UnityEngine.Component component, .PhotonStream stream, .PhotonMessageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:367)
PhotonView.DeserializeView (.PhotonStream stream, .PhotonMessageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:352)
NetworkingPeer.OnSerializeRead (ExitGames.Client.Photon.Hashtable data, .PhotonPlayer sender, Int32 networkTime, Int16 correctPrefix) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3498)
NetworkingPeer.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1924)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte[] inBuff)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands ()
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands ()
PhotonHandler.Update () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)

Hope you can help me,

Thank you.

After your game hits a NullRef the rest of the code in that script basically doesn’t run. There’s no point ignoring one as it leads to many other problems and even worse, leads to you ‘fix’ problems that don’t exist.

As to your error, this QA reckons import it (using/import) instead of extending from it.

http://answers.unity3d.com/questions/317916/photonnetwork-javascript.html

I am pretty sure the error is where you call the rpc, I think it should be hit.transform. root.GetComponent … But thats just me. :wink: