Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CStampGames · Jan 09, 2018 at 02:53 AM · networkingmultiplayerscalesynchronizationchild object

UNet - sync child transform scale

How can I use the Unet framework to sync the localScale changes of a spawned GameObject's children??

In my game, my players meshes belong to a "PlayerModel" child of my "Player" gameobject. I want people to be able to customize their players by adjusting the scaling of the playermodel object before coming into the game but can't get it to sync right... It works well enough whenever I try to alter the "Player" object itself but that also alters things that I don't want altered. I've tried using SyncVar, I've tried using Rpcs and I feel like I've tried about everything else... Here's my code:

(also btw the data is stored on an external server - hence the "OnRecievedData" function and all that jazz... don't worry about any of that, it works fine")

public class PlayerCustoms : NetworkBehaviour {

 [SyncVar]
 [SerializeField] 
 GameObject playerModel;

 AccountManager accountManager;
 GameManager gameManager;
 InGameData inGameData;
 PlayerDataHandler dataHandler;

 public string data = "";

 Player[] players;

 private NetworkIdentity objNetID;

 bool dataRecieved = false;

 [SyncVar]
 public Vector3 scale = new Vector3 (1, 1, 1);

 void Start(){
     accountManager = AccountManager.GetInstance ();
     gameManager = GameManager.GetInstance ();
     dataHandler = PlayerDataHandler.GetInstance ();
     inGameData = InGameData.GetInstance ();

     SetUniqueData ();
 }
     
 public void SetUniqueData(){

     dataRecieved = true;
     accountManager.InvokeGetData (OnDataRecieved);

 }

 void OnDataRecieved(string data){
     this.data = data;
     dataRecieved = true;
     inGameData.allCustomDimensions.Add (GetComponent<Player> ().username, DataTranslator.DataToDimensions (data));
     print ("Data recieved: " + data);
     print ("New dimensions will be " + DataTranslator.DataToDimensions (data));

     dataRecieved = true;

     Vector3 newScale = DataTranslator.DataToDimensions (data);

     SetScale (newScale);

 }

 void SetScale(Vector3 scale){
     if (isLocalPlayer) {

         playerModel = transform.Find ("Graphics").transform.Find ("PlayerModel").gameObject;
         print ("Found playerModel: " + playerModel);

         CmdScale (playerModel, scale);
     }
 }

 [Command]
 void CmdScale(GameObject playerModel, Vector3 scale){
     Debug.Log ("CMDSCALE");

     objNetID = playerModel.GetComponent <NetworkIdentity> ();

     //if (isServer) {
     //    objNetID.AssignClientAuthority (connectionToClient);

         RpcScale (playerModel, scale);

     //    objNetID.RemoveClientAuthority (connectionToClient);
     //}
 }

 [ClientRpc]
 void RpcScale(GameObject playerModel, Vector3 scale){
     Debug.Log ("RPCSCALE");
     playerModel.transform.localScale = scale;
     Debug.Log ("Set scale for " + playerModel + " to " + scale);

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by CStampGames · Feb 19, 2018 at 02:44 AM

solved it

public class PlayerCustoms : NetworkBehaviour {

 [SerializeField]
 Transform playerModel;

 void Start(){
     Player[] players = GameManager.GetInstance ().GetAllPlayers ();

     foreach (Player player in players) {
         player.GetComponent <PlayerCustoms> ().CustomizeLocal ();
     }
 }

 [Client]
 public void CustomizeLocal (){
     
     if (!isLocalPlayer)
         return;

     Vector3 scale = DataTranslator.DataToDimensions (InGameData.GetInstance ().dataDic [AccountManager.GetInstance ().username]);
     Color[] colors = DataTranslator.DataToColor (InGameData.GetInstance ().dataDic [AccountManager.GetInstance ().username]);

     CmdOnCustomize (scale, colors);

 }

 [Command]
 void CmdOnCustomize(Vector3 scale, Color[] colors){
     RpcOnCustomize (scale, colors);
 }

 [ClientRpc]
 void RpcOnCustomize(Vector3 scale, Color[] colors){
     ApplyCustomization (scale, colors);
 }

 private void ApplyCustomization(Vector3 scale, Color[] colors){
     playerModel.localScale = scale;

     for (int i = 0; i < colors.Length; i++) {
         playerModel.GetChild (i).GetComponent <MeshRenderer> ().material.color = colors [i];
         playerModel.GetChild (i).GetComponent <MeshRenderer> ().material.SetColor ("_EmissionColor", colors [i]);
     }

 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mirror networking, weird behaviour with local scale 0 Answers

Setting function to run on all clients but only work on the player that called it 0 Answers

[uNET] Instantiated gameobjects not syncing for new clients 1 Answer

Executing command in a child GameObject in a multiplayer Game 2 Answers

How to synchronize an object in an online game? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges