I am currently trying to figure out how to respawn a character after they have “died” with Photon. I have tried a variety of ways, and the camera always “freezes” after I run this code… The game does not encounter any errors, but the character is “destroyed”, so the camera is inactive and the game just freezes, owing to no camera.
Here is my code, please give suggestions!
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
SpawnSpot[] spawnSpots;
public float hitPoints = 100f;
float currentHitPoints;
GameObject standbyCamera;
public int spawnTime = 3;
// Use this for initialization
void Start () {
spawnSpots = FindObjectsOfType<SpawnSpot>();
currentHitPoints = hitPoints;
}
[RPC]
public void TakeDamage(float amt) {
currentHitPoints -= amt;
if(currentHitPoints <= 0) {
Die();
}
}
void Die() {
if(GetComponent<PhotonView>().instantiationId == 0){
Destroy(gameObject);
}
else {
/*if(PhotonNetwork.isMasterClient) {
PhotonNetwork.Destroy(gameObject);
}
else {
Debug.Log ("Not MasterClient! You just wait patiently!");
}*/
/*if(PhotonNetwork.isMasterClient) {
PhotonNetwork.Destroy (gameObject);
}*/
standbyCamera = GameObject.Find ("spectatorCamera");
gameObject.transform.FindChild("Main Camera").gameObject.camera.enabled = false;
//Respawn ();
}
}
void Respawn() {
SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0, spawnSpots.Length)];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
//((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("PlayerMovement")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("PlayerShooting")).enabled = true;
//((MonoBehaviour)myPlayerGO.GetComponent("CharacterMotor")).enabled = true;
myPlayerGO.transform.FindChild("Main Camera").gameObject.camera.enabled = true;
}
}