Instantiate GameObject with instantiater's color

I'm trying to color a player's dead body the same color they were, before they died.
using UnityEngine;
using System.Collections;

public class PlayerHealthScript : Photon.MonoBehaviour {

	private GameObject myDeadPlayer;
	public int currentHealth;
	public int maxHealth;
	public bool dead = false;
	public GameObject focusPoint;
	public Vector3[] respawnPoints;

	public GameObject pistol;

	void Start () 
	{
		currentHealth = maxHealth;
	}
	
	void Update () 
	{
		if (currentHealth <= 0 && !dead) 
		{
			myDeadPlayer = (GameObject)PhotonNetwork.Instantiate("DeadPlayer", transform.position, Quaternion.identity, 0);

			var randomX = Random.Range (-150, 150);
			var randomY = 0;
			var randomZ = Random.Range (-150, 150);
			var randomDir = new Vector3 (randomX, randomY, randomZ);
			
			myDeadPlayer.rigidbody.velocity = randomDir * 1 * Time.deltaTime;

			photonView.RPC ("Death", PhotonTargets.AllBuffered, null);
		}
	}
	
	void OnGUI() 
	{
		if (photonView.isMine)
		{
			GUI.Label(new Rect(10, 10, 150, 50), "Health: " + currentHealth);
			GUI.skin.label.fontSize = 20;
		}
	}

	[RPC]
	void TakeDamage(int amt)
	{
		currentHealth -= amt;
	}

	[RPC]
	void Death ()
	{
		dead = true;
		currentHealth = 0;
		gameObject.SetActive (false);
		myDeadPlayer.renderer.material.color = this.renderer.material.color;

		if (photonView.isMine) 
		{
			SmoothFollowScript camScript = Camera.main.GetComponent<SmoothFollowScript> ();
			camScript.target = myDeadPlayer.transform;
		}

		Invoke ("GetReadyToRespawn", 5);
	}

	void GetReadyToRespawn ()
	{
		photonView.RPC ("Respawn", PhotonTargets.AllBuffered, null);
	}

	[RPC]
	void Respawn ()
	{
		dead = false;
		currentHealth = maxHealth;
		gameObject.SetActive (true);

		if (photonView.isMine) 
		{
			transform.position = respawnPoints[Random.Range(0,respawnPoints.Length)];
			SmoothFollowScript camScript = Camera.main.GetComponent<SmoothFollowScript> ();
			camScript.target = focusPoint.transform;
		}

		PistolShootingScript pistolShootingScript = pistol.GetComponent<PistolShootingScript> ();
		pistolShootingScript.currentAmmo = pistolShootingScript.maxAmmo;
	}
}

I just simply added this.
myDeadPlayer.renderer.material.color = this.renderer.material.color;

But I keep getting this error, when the player is killed.
NullReferenceException: Object reference not set to an instance of an object
PlayerHealthScript.Death () (at Assets/Scripts/PlayerHealthScript.cs:58)
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)
NetworkingPeer.ExecuteRPC (ExitGames.Client.Photon.Hashtable rpcData, .PhotonPlayer sender) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1937)
NetworkingPeer.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1643)
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:76)

I've really tried everything, not sure what the heck is going on...
Please help.

Comments

  • I don't mean to rush you guys, I know you're trying to help everyone, which is very respectable.

    But this has sorta stopped development for me, I just can't get this working. I promised my folks I'd have color-able players by tonight..

    Hope you can help me! :)
  • Still need help with this.. D:
  • Hi,

    What line is Assets/Scripts/PlayerHealthScript.cs:58 and which reference is null?
    I suppose that this is myDeadPlayer. Probably that var is not initialized on remote clients assuming that PhotonNetwork.Instantiate("DeadPlayer") called by only one client. Right?
  • You should network-update DeadPlayer properties in PhotonView script attached to DeadPlayer, not in other's objects scripts. That way, you do not need references to DeadPlayer when updating (but they still may be useful on owner client to set this properties).