Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Toggle object between server and client

Discussion in 'Multiplayer' started by dil33pm, Mar 29, 2016.

  1. dil33pm

    dil33pm

    Joined:
    Aug 5, 2015
    Posts:
    5
    I am making a game with UNET that has only two devices (two players). There will be one gameObject that both the players will interact, which will be present in either server side (player 1) or client side (player 2) at any point of time. I don't want both the devices to spawn the gameObject.

    How can I toggle the visibility of the gameObject in client, if a bool is true? Please help, it is quite urgent. The bool is a SyncVar

    Code (CSharp):
    1.     //P1: Server = true, P2: Client = false
    2.     [SyncVar(hook = "jump")]
    3.     public bool where = true; //is in server
    I am trying this piece of code, but its not working.

    Code (CSharp):
    1.     void jump(bool where)
    2.     {
    3.         Debug.Log("WHERE changed to " + where + " isServer? " + isServer);
    4.  
    5.         if (isServer && where)
    6.             GameObject.Find("player(Clone)").GetComponent<SpriteRenderer>().enabled = where;
    7.         else
    8.             GameObject.Find("player(Clone)").GetComponent<SpriteRenderer>().enabled = false;
    9.     }
     
    Last edited: Mar 29, 2016
  2. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    450
  3. dil33pm

    dil33pm

    Joined:
    Aug 5, 2015
    Posts:
    5
    This triggers the visibility when the player prefab is nearby. My scenario is more like there is only ONE object in the whole game. Both clients and server can interact with it (individual player prefabs won't be there in the game scene).
     
  4. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    You need to have the object spawned on both to be able to use it with the high level networking stuff(SyncVars, Command, etc.). If you are only using a single object on both sides, then you will need to use messages to control it properly, since the client won't be able to do anything to it otherwise.