Search Unity

Synchronizing setParent?

Discussion in 'Multiplayer' started by RobotMaker12, May 14, 2017.

  1. RobotMaker12

    RobotMaker12

    Joined:
    Feb 15, 2016
    Posts:
    2
    Hello,
    I'm testing out Unity Networking and I have a script that sets my player's parent as the Canvas since my spawn points, which are in the canvas, won't make my player spawn there. However, I can't get this SetParent() to sync across games. The script in question is this :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : NetworkBehaviour {
    6.    
    7.     void Start () {
    8.         // Place the Player into the Canvas
    9.         GameObject.Find("Player(Clone)").GetComponent<Image>().transform.SetParent(GameObject.Find("Canvas").GetComponent<Canvas>().transform);
    10.     }
    11. }
    12.  
    Since this is a transform, I added a Network Transform to my player prefab, but it still won't sync...

    What could be the issue?

    Thanks!
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    What you want to do is to sync the parent name in some form of syncVar (string). Then you would setParent on your StartMethod. Personally used this post as reference:
    http://stackoverflow.com/questions/...bject-as-a-child-for-the-host-and-all-clients
     
  3. RobotMaker12

    RobotMaker12

    Joined:
    Feb 15, 2016
    Posts:
    2
    Something like this? Unfortunately, it's still not syncing across games... I'm also getting a warning saying "Trying to send command for object without authority."
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class PlayerController : NetworkBehaviour {
    7.  
    8.     [SyncVar]
    9.     public string parent = "Canvas";
    10.  
    11.     void Start () {
    12.         CmdSetParent(parent);
    13.         // Place the Player into the Canvas
    14.         GameObject.Find("Player(Clone)").GetComponent<Image>().transform.SetParent(GameObject.Find(parent).GetComponent<Canvas>().transform);
    15.     }
    16.  
    17.     [Command]
    18.     public void CmdSetParent(string newParent)
    19.     {
    20.         parent = newParent;
    21.     }
    22. }
    Thanks!

    Edit : I apologize if the code is way off... I have almost no experience in Unity Networking.
     
    Last edited: May 16, 2017
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    The objects you are spawning as children. Make sure their network identity is "localPlayerAuthority". And use the NetworkServer.SpawnWithAuthority and not the standard NetworkServer.Spawn. You can only send commands from objects you have authority over.
     
    Last edited: May 17, 2017