Search Unity

[Solved] Network gameobject activation / deactivation not working

Discussion in 'Multiplayer' started by QuinnWinters, Oct 30, 2016.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    313
    I'm attempting to activate and deactivate objects over the network but it only seems to want to work for the local player that the objects are attached to. What am I doing wrong here?

    Edit: Further experimentation has shown me that a deactivated gameobject can not be seen over the network. That led to the conclusion that the object's individual components need to be deactivated instead, but that has the same limitation. Once I deactivate a component I can't reactivate it over the network. How then, do I accomplish the task on the other end of the network?

    Edit 2: Someone on reddit suggested that I place the objects into a list and pass a non-gameobject variable such as an integer over the network and to then pull the corresponding item from that list on each network client and that did the trick. Updated working script below the original script.

    Code (CSharp):
    1. // parentObject is a child of a player prefab with local player authority and contains two
    2. // or more child objects with different names, one of which is selected in the inspector.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6. using UnityEngine.Networking;
    7.  
    8. public class Notes : NetworkBehaviour {
    9.  
    10.     public GameObject parentObject;
    11.     public List <GameObject> childObject = new List <GameObject>();
    12.     GameObject selectedObject;
    13.  
    14.     void FixedUpdate () {
    15.  
    16.         if (!isLocalPlayer) {
    17.             return;
    18.         }
    19.  
    20.         if (Input.GetKeyDown ("1")) {
    21.             selectedObject = childObject;
    22.             CmdSwitch ();
    23.         }
    24.         if (Input.GetKeyDown ("2")) {
    25.             selectedObject = null;
    26.             CmdSwitch ();
    27.         }
    28.  
    29.     }
    30.  
    31.     [Command]
    32.     public void CmdSwitch () {
    33.  
    34.         Switch ();
    35.         RpcSwitch ();
    36.  
    37.     }
    38.  
    39.     [ClientRpc]
    40.     void RpcSwitch () {
    41.  
    42.         Switch ();
    43.  
    44.     }
    45.  
    46.     public void Switch () {
    47.  
    48.         //If the child is selected activate it, otherwise deactivate all children
    49.         if (selectedObject != null) {
    50.             foreach (Transform child in parentObject.transform) {
    51.                 if (child.gameObject.name == selectedObject.name) {
    52.                     child.gameObject.SetActive (true);
    53.                 }
    54.             }
    55.         }
    56.         else {
    57.             foreach (Transform child in parentObject.transform) {
    58.                 child.gameObject.SetActive (false);
    59.             }
    60.         }
    61.  
    62.     }
    63.  
    64. }
    Updated script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.Networking;
    5.  
    6. public class Notes : NetworkBehaviour {
    7.  
    8.     public List <GameObject> childObject = new List <GameObject>();
    9.  
    10.     void FixedUpdate () {
    11.  
    12.         if (!isLocalPlayer) {
    13.             return;
    14.         }
    15.  
    16.         if (Input.GetKeyDown ("1")) {
    17.             CmdSwitch (1);
    18.         }
    19.         if (Input.GetKeyDown ("2")) {
    20.  
    21.             CmdSwitch (2);
    22.         }
    23.  
    24.     }
    25.  
    26.     [Command]
    27.     public void CmdSwitch (int objectNumber) {
    28.  
    29.         Switch (objectNumber);
    30.         RpcSwitch (objectNumber);
    31.  
    32.     }
    33.  
    34.     [ClientRpc]
    35.     void RpcSwitch (int objectNumber) {
    36.  
    37.         Switch (objectNumber);
    38.  
    39.     }
    40.  
    41.     public void Switch (int objectNumber) {
    42.  
    43.         //Activate the selected object, deactivate the others
    44.         if (objectNumber == 1) {
    45.             childObject [0].SetActive (true);
    46.             childObject [1].SetActive (false);
    47.         }
    48.         if (objectNumber == 2) {
    49.             childObject [0].SetActive (false);
    50.             childObject [1].SetActive (true);
    51.         }
    52.  
    53.     }
    54.  
    55. }
     
    Last edited: Oct 31, 2016
    Jeryxem likes this.
unityunity