Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SetActive a gameObject over RPC

Discussion in 'Multiplayer' started by ManitoBart, Apr 24, 2015.

  1. ManitoBart

    ManitoBart

    Joined:
    Dec 10, 2014
    Posts:
    12
    Hi,

    I'm stuck for a while on this : I try to send a RPC on a GameObject to say "hey SetActive your child!" but it doesn't work in other players screen.

    My code is like this :

    public Transform theTransformOfMyChild;

    GetComponent<NetworkView>().RPC("ActiveThisThing", RPCMode.All);

    [RPC]
    void ActiveThisThing ()
    {
    theTransformOfMyChild.gameObject.SetActive(true);
    }


    NetworkView are on the object wich have the script, I try to set it to observe the script, the transform, "unreliable and all the other way but nothing worked !

    My RPCs work fine for instantiation but not for SetActive, Is that normal ?

    Please I need HELP ....
     
  2. ManitoBart

    ManitoBart

    Joined:
    Dec 10, 2014
    Posts:
    12
    Ok, i've made a simple test with a cube to appear and it works great, so there's a problem in my script.
    If someone find something weard, there it is :

    public int currentWeapon = 0;
    public int maxWeapons = 2;
    public Transform WeapA;
    public Transform WeapB;
    public Transform WeapC;
    public NetworkView nView;

    // Use this for initialization
    void Start () {

    SelectWeapon (0);
    nView = GetComponent<NetworkView> ();

    }

    // Update is called once per frame
    void Update ()
    {

    if (Input.GetAxis ("Mouse ScrollWheel") < 0)
    {
    if (currentWeapon + 1 <= maxWeapons)
    {
    currentWeapon ++;
    }
    else
    {
    currentWeapon = 0;
    }
    SelectWeapon (currentWeapon);
    }
    else if (Input.GetAxis ("Mouse ScrollWheel") > 0)
    {
    if (currentWeapon - 1 >= 0)
    {
    currentWeapon--;
    } else {
    currentWeapon = maxWeapons;
    }
    SelectWeapon (currentWeapon);
    }
    if (currentWeapon == maxWeapons + 1)
    {
    currentWeapon = 0;
    }
    if (currentWeapon == -1)
    {
    currentWeapon = maxWeapons;
    }
    if (Input.GetKeyDown (KeyCode.Alpha1))
    {
    currentWeapon = 0;
    SelectWeapon (currentWeapon);
    }
    if (Input.GetKeyDown (KeyCode.Alpha2))
    {
    currentWeapon = 1;
    SelectWeapon (currentWeapon);
    }
    if (Input.GetKeyDown (KeyCode.Alpha3))
    {
    currentWeapon = 2;
    SelectWeapon (currentWeapon);
    }


    }

    void SelectWeapon (int index)
    {
    if (GetComponent<NetworkView>().isMine){
    for (int i = 0; i < transform.childCount; i ++)
    {
    //Activate the selected weapon
    if ( i == index)
    {


    if ( transform.GetChild (i).name == "WeapA)
    {
    nView.RPC("GetWeapA", RPCMode.All);

    }

    if ( transform.GetChild (i).name == "WeapB")
    {

    nView.RPC("GetWeapB", RPCMode.All);

    }

    if ( transform.GetChild (i).name == "BallOfPee")
    {

    nView.RPC("GetWeapC", RPCMode.All);

    }




    }
    }
    }
    }

    [RPC]
    void GetWeapA ()
    {

    WeapA.gameObject.SetActive (true);
    WeapB.gameObject.SetActive (false);
    WeapC.gameObject.SetActive (false);

    }

    [RPC]
    void GetWeapB ()
    {

    WeapA.gameObject.SetActive (false);
    WeapB.gameObject.SetActive (true);
    WeapC.gameObject.SetActive (false);

    }

    [RPC]
    void GetWeapC ()
    {

    WeapA.gameObject.SetActive (false);
    WeapB.gameObject.SetActive (false);
    WeapC.gameObject.SetActive (true);

    }
    }
     
    Last edited: Apr 27, 2015