How to run a RPC Coroutine?

Options
Hi, I am trying to make a bullet script where 2 sounds play after the other after a certain amount of time after the bullet is shot so than all players can hear it but it seems like when I do photonView.RPC("Coroutine", RpcTarget.All) it does not work because it is not starting the coroutine and it just spams my console when it gets to the yield return new WaitForSeconds()

here is my code
void Update()
    {
        if (photonView.IsMine)
        {
            if (Input.GetKey(KeyCode.Mouse1))
            {
                scope.SetActive(true);
                crosshair.SetActive(false);
                if (Input.GetKeyDown(KeyCode.Mouse0) && !isPlaying)
                {
                    isPlaying = true;
                    photonView.RPC("RPC_PlaySound", RpcTarget.All);
                }
            }
            else
            {
                scope.SetActive(false);
                crosshair.SetActive(true);
            }
        }
    }

    [PunRPC]
    IEnumerator RPC_PlaySound()
    {
        AudioSource sound = gun.transform.Find("Sound1").GetComponent<AudioSource>();
        AudioSource sound2 = gun.transform.Find("Sound2").GetComponent<AudioSource>();
        flashEffect.Play();
        photonView.RPC("RPC_Fire", RpcTarget.All);
        sound.Play();
        yield return new WaitForSeconds(2);
        sound2.Play();
        yield return new WaitForSeconds(0.5f);
        isPlaying = false;
    }

    [PunRPC]
    public void RPC_Fire()
    {
        bullet = Instantiate(bulletPrefab);
        bullet.transform.position = bulletSpawn.position;
        Vector3 rotation = bullet.transform.rotation.eulerAngles;
        bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
        bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
        StartCoroutine(DestroyBulletAfterTime(bullet, lifeTime));
    }

    IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
    {
        yield return new WaitForSeconds(delay);
        Destroy(bullet);
    }
This has been making me pull my hair out for ages and I am unsure how this would work

Comments

  • redi
    Options

    Probably it is not starting coroutine, cause u didn't call StartCoroutine for RPC_PlaySound. Does it work for DestroyBulletAfterTime ?