An annoyance With PhotonNetwork.Destroy()

Hey Guys! I am trying to find a way to fix my Issue. I do not know why but for some stupid reason Photon Decided to not add a time parameter to PhotonNetwork.Destroy So I have to find a way around delayed destruction; I am trying to make a gun that shoots (Which is simple enough) but not in Photon..

Here is my code so far..
[code]using UnityEngine;
using System.Collections;

public class Attack : MonoBehaviour
{

public GameObject Bullet_Emitter;

public float Bullet_Forward_Force;


void Start()
{

}


float maxTimer = 1;
float maxReloadTimer = 5;
float timer = 0;
float reloadTimer;
float intervealTimer;
float maxIntervealTimer = 2;
int shotsTaken = 0;
bool timerIsOn = false;
bool reloadFase = false;
bool Shoot = false;

void Update()
{
intervealTimer -= Time.deltaTime;
if (reloadFase)
reloadTimer -= Time.deltaTime;

if (timerIsOn)
timer -= Time.deltaTime;

if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (!reloadFase)
{
shotsTaken++;
timer = maxTimer;
timerIsOn = true;
Debug.Log("Shots" + shotsTaken);
Debug.Log("Timer" + timer);
Shoot = true;
}
else
{
Debug.Log("ReloadFase On");
Debug.Log("Reload Timer" + reloadTimer);
}
}

if (shotsTaken == 3)
{
reloadTimer = maxReloadTimer;
reloadFase = true;
shotsTaken = 0;
}

if (reloadTimer < 0 && reloadFase == true)
{
reloadFase = false;
reloadTimer = 0;
}

if (Shoot && intervealTimer < 0)
{

GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = PhotonNetwork.Instantiate("projectile", Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation, 0) as GameObject;
intervealTimer = maxIntervealTimer;
Shoot = false;
}

if (timer < 0 && timerIsOn == true)
{
PhotonNetwork.Destroy();
timerIsOn = false;
timer = 0;
}
}
}

[/code]
My problem is on line 80, as you can see I cannot get Temporary_Bullet_Handler.. this would be much more simpler if Photon added the delay feature. What the script is in short:
The player can shoot in 3 bursts (shotsTaken) once shotsTaken is true, reloadFase turns true which turns on the reload timer, reloading it. The timer is how long it takes to destroy the bullet, and timerIsOn is just double checking that the timer is on. I have no real way of despawning the bullet with a delay (Well of to my knowledge) Please Help!

Comments

  • Sorry, didn't realize this forums didn't show up the code box, line 80 is 7th from bottom (PhotonNetwork.Destroy())
  • Could you add a Coroutine to the bullet and let it begin with a yield WaitForSeconds? After that time, it could call PN.Destroy()?
    This should simplify the timing and organization, I guess.
  • Never really learnt how to use yields and corountines but I'll learn it
  • PhotonNetwork.Destroy in a coroutine doesnt work any solution? the code doesnt get called thanks
  • I found a cool solution after spending some time on this.

    Basically I call an rpc for taking damage and inside of it I destroy the game object and call a Spawn() function. In the argument I passed in the time to delay before respawning like so:

    [PunRPC]
    private void RPCTakeDamage(float _damage)
    {
    if (!photonView.IsMine)
    return;

    currentHealth -= _damage;

    if(currentHealth <= 0f)
    {
    PhotonNetwork.Destroy(gameObject);
    spawnManager.SpawnPlayer(2f);
    }
    }

    The spawn function is in another script which does this:

    public void SpawnPlayer(float time)
    {
    if (PhotonNetwork.IsMasterClient)
    {
    StartCoroutine(Wait(time, 0));
    }
    else
    {
    StartCoroutine(Wait(time, 1));
    }
    }

    IEnumerator Wait(float seconds, int index)
    {
    yield return new WaitForSeconds(seconds);
    PhotonNetwork.Instantiate(player.name, spawnPoints[index].position, spawnPoints[index].rotation);
    }

    It works for me but it does throw a random error which doesn't really impact the game at all.
    Hope this helps