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

Want to send command from script attached on non-player object

Discussion in 'Multiplayer' started by llmen, Feb 23, 2017.

  1. llmen

    llmen

    Joined:
    Jan 25, 2017
    Posts:
    1
    Hi All,

    Basically, I am doing a multiplayer sequencer. There are four buttons, by pressing them, the buttons could be switched on or off. I plan to use syncvar to sync the condition of each button form server to client, and when the button is clicked on the client side, I plan to use command to send request to change the button condition on the server. This works perfect if the command is sent from player controller which is attached on local player. However, due to the button is not clicked by the player object, it was clicked by my mouse directly, it command can hardly be sent from local player controller. Any thoughts? Below is the code in which the sequencer root object was spawned, it is also the parent object of all the buttons, it is also on it that I attach another script and try to send command to update button conditions. The sequencer could be generrated automatically once the client connects the server, however, I cannot send command from non-player object. I tried NetworkServer.SpawnWithClientAuthority (StepSequencer, playerPrefab), still error, saying SpawnWithClientAuthority player object is not a player. How shall I achieve my aim, each button's condition could be changed from both client and server, at the same time, keep synced. Thanks so much.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class SequencerSpawner : NetworkBehaviour {
    5.  
    6.     public GameObject SequencerPrefab;
    7.     public Transform SequencerPosition;
    8.     //public int numberOfEnemies;
    9.  
    10.     public override void OnStartServer() //run once the server is started
    11.     {
    12.         var StepSequencer = (GameObject)Instantiate(SequencerPrefab, new Vector3(-5.75f,6.29f,0f), Quaternion.Euler(0,0,0));
    13.             NetworkServer.Spawn(StepSequencer);  
    14.     }
    15. }