get player object in scene

hi! i make fps game. I have a problem. I need to make a spawn shield but I can't do anything. I have a player on the disabled object, "the shield". I need to turn it on, when a player appears for 3 seconds. I did everything: timer function enable, rpc. but one question: when performing rpc, I have to find the scene object with the player, but I don't know how. so I tried to do this:

IEnumerator stime()
{
shiled = 4;
while(shiled > 0)
{
shiled--;

ens ();

yield return new WaitForSeconds(1);
}

dis();
}

ens void()
{
photonView.RPC ("ensh", PhotonTargets.Others, PhotonNetwork.playerName);
}
void dis()
{
photonView.RPC ("dish", PhotonTargets.Others, PhotonNetwork.playerName);
}

[PunRPC]
void ensh(string name)
{
tar = GameObject.Find (name);
//Debug.Log (name);
star = tar.gameObject.transform.GetChild (4).gameObject;
//star = name.transform.GetChild (4).gameObject;
star.SetActive (true);
}

[PunRPC]
void dish(string name)
{
tar = GameObject.Find (name);
star = tar.gameObject.transform.GetChild (4).gameObject;
//star = name.transform.GetChild (4).gameObject;
star.SetActive (false);
}

he gave the name of the player. in the scene the player object with the same name as the name in the photon
«1

Answers

  • can you explain is this shield on the level or ?
  • no, on the player prefab
  • i do not get your code so you need to turn something for 4 second and then disable it ? something like this ?
  • ens void()
    {
    photonView.RPC ("ensh", PhotonTargets.Others, PhotonNetwork.playerName);
    }

    void ensh(string name)
    {
    tar = GameObject.Find (name);
    //Debug.Log (name);
    star = tar.gameObject.transform.GetChild (4).gameObject;
    //star = name.transform.GetChild (4).gameObject;
    star.SetActive (true);
    }
  • its screenshot
  • for example if you want to make some effect for yourself and for others to see this you can make something like this :
    void EnableEffect()
    {
    photonView.RPC("ShowEffect",PhotonTargets.All);
    }

    [PunRPC]
    void ShowEffect()
    {
    // here code for showing if the effect is same you can remove checking isMine
    if(photonView.isMine)
    {
    //for local player
    }
    else
    {
    //for other players too see this
    }
    }
  • you do not need to send player name its your player and your photon view and it will get all your copies

  • its screenshot of player prefab
  • but how i can activate object?
  • and disable function can be called in ShowEffect by calling Invoke();
  • transform.getchild(4).gameobject.setactive(true);
  • no! i want to enable shield on another player!
  • it`s enable shield on me, but other players can`t see it.
  • on copy of yourself in other players PC ?
  • the code that i have send will activate something on yourself locally and for your copy

  • its me on my client


  • but its me on another client

  • on another
  • can you make code that i wrote to set on some key down and you will see its working :)
  • on copy of yourself in other players PC ? yes, yes! i want to activate shield on my copy on other players pc
  • ok
  • but i need to enable shield on my copie localy?
  • [PunRPC]
    void ShowEffect()
    {
    gameobject.setactive(true);
    }

    this will activate on local and client
  • i`ll try it
  • nooo! not working
  • its activate shield locally, on my pc. but on other pc shield on my copie not activated
  • this.StopCoroutine ("stime");
    this.StartCoroutine ("stime");
    void ens()
    {
    photonView.RPC ("ensh", PhotonTargets.All);
    }
    void dis()
    {
    photonView.RPC ("dish", PhotonTargets.All);
    }

    IEnumerator stime()
    {
    shiled = 4;
    while(shiled > 0)
    {
    shiled--;
    print ("shild" + shiled);
    ens();

    yield return new WaitForSeconds(1);
    }

    dis();
    Debug.Log ("sh dis");

    }
    [PunRPC]
    void ensh()
    {
    //tar = GameObject.Find (name);
    //Debug.Log (name);
    //star = tar.gameObject.transform.GetChild (4).gameObject;
    //star = name.transform.GetChild (4).gameObject;
    //star.SetActive (true);
    ourPlayer.transform.GetChild(4).gameObject.SetActive(true);
    }

    [PunRPC]
    void dish()
    {
    //tar = GameObject.Find (name);
    //star = tar.gameObject.transform.GetChild (4).gameObject;
    //star = name.transform.GetChild (4).gameObject;
    //star.SetActive (false);
    ourPlayer.transform.GetChild(4).gameObject.SetActive(false);
    }
  • can you try AllBuffered
  • i will post Demo so you can see that everything is working
  • here is link
    https://www.dropbox.com/s/tyqaqifsiyr7uzl/DemoWorker.rar?dl=0
    replace this forlder in PUNs Unity folder. I have changed PlayerNetworkController look at Update function. everything is working only thing is if you want to notify players that join after you have disabled effect use AllBuffered when Calling RPC