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 /
avatar image
2
Question by Mike_Sp · Nov 01, 2015 at 10:44 PM · gameobjectnetworkingmaterialunity5

Networking change of GameObject properties (color, size, etc)

Hello!

I'm working on and learning some basics of Unity 5, UNET, and networking. I made a simple 3D game where you go around and change the colors of objects. But I want to make it multiplayer now, and I am having lots of trouble figuring out how to send the changes over the network so all players can see a single player's color change.

Part of the issue is that it has been difficult to find the answer using the newer UNET networking engine. And sometimes I come across answers that are for the older way.

So the main question is, how do I network non-player GameObject property changes? Color, shape, size, etc..

Here is some code I have now - and I've had many different versions so I'll just post the current one:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class Player_Paint : NetworkBehaviour {
 
     private int range = 200;
     [SerializeField] private Transform camTransform;
     private RaycastHit hit;
     [SyncVar] private Color objectColor;
     [SyncVar] private GameObject objIdentity;
     
     void Update () {
         CheckIfPainting();
     }
 
     void CheckIfPainting(){
         if(Input.GetMouseButtonDown(0)) {
             if (Physics.Raycast (camTransform.TransformPoint (0, 0, 0.5f), camTransform.forward, out hit, range)) {
                 string objName = hit.transform.name;
             CmdPaint(objName);
             }
         }
     }
 
     [ClientRpc]
     void RpcPaint(){
         objIdentity.GetComponent<Renderer>().material.color = objectColor;
     }
 
     [Command]
     void CmdPaint(string name) {
     objIdentity = GameObject.Find (name);  //tell us what was hit
     objectColor = new Color(Random.value, Random.value, Random.value, Random.value);
     RpcPaint ();
     }
 }

Edited: updated [ClientRpc] and [Command] functions. But still not working. It seems like the server is updated, but the client is not.

Comment
Add comment · Show 3
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
avatar image Mike_Sp · Nov 02, 2015 at 02:14 AM 0
Share

I've tried a bunch more solutions, including writing a separate script on the objects whose color I want to change and including [SyncVar] and hook functions. I've also tried Debug.Log on each of the functions I'm expecting to update the objects on the clients and they are executing with the expected data.

I really don't know what else to do. I feel like it is a VERY simple thing I want to do, but I haven't come across syncing non-player GameObject's in any questions, tutorials, or other resources. Any ideas at all would be helpful, thank you.

avatar image I-am-luka · Feb 24, 2016 at 08:43 AM 0
Share

Thx this post helped me a lot. Here is a simple optimisation of this code : objectID = GameObject.Find (hit.transform.name); you can simply do : objectId = hit.transform.gameObject

avatar image chethan-s · Aug 24, 2016 at 11:11 AM 0
Share

Hello $$anonymous$$ike, I am facing similar problem but was not able to overcome it if you can send me a simple project which changes the properties of a npc object it would be really helpfull

Thank you

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Mike_Sp · Nov 04, 2015 at 08:06 AM

I figured it out:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class Player_Paint : NetworkBehaviour {
 
     private int range = 200;
     [SerializeField] private Transform camTransform;
     private RaycastHit hit;
     [SyncVar] private Color objectColor;
     [SyncVar] private GameObject objectID;
     private NetworkIdentity objNetId;
     
     void Update () {
         if (isLocalPlayer) {    
             CheckIfPainting ();
         }
     }
 
     void CheckIfPainting(){
         if(isLocalPlayer && Input.GetMouseButtonDown(0)) {
             if (Physics.Raycast (camTransform.TransformPoint (0, 0, 0.5f), camTransform.forward, out hit, range)) {
                 objectID = GameObject.Find (hit.transform.name);                                    // this gets the object that is hit
                 objectColor = new Color(Random.value, Random.value, Random.value, Random.value);    // I select the color here before doing anything else
                 CmdPaint(objectID, objectColor);
             }
         }
     }
     
     [ClientRpc]
     void RpcPaint(GameObject obj, Color col){
         obj.GetComponent<Renderer>().material.color = col;        // this is the line that actually makes the change in color happen
     }
     
     [Command]
     void CmdPaint(GameObject obj, Color col) {
         objNetId = obj.GetComponent<NetworkIdentity> ();        // get the object's network ID
         objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player who is changing the color
         RpcPaint (obj, col);                                    // usse a Client RPC function to "paint" the object on all clients
         objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player who changed the color
     }
 }

I had to use the "AssignClientAuthority" and "RemoveClientAuthority" and it works now!!! Important things to note: The objects which you wish to affect must have the NetworkIdentity component attached, and that must be set to LocalPlayerAuthority (If you forget, the warning errors are helpful to remind you).

This should work for any change you want to make on a non-player object that you want to sync with all clients. This example just changes the object to a random color.

Comment
Add comment · Show 2 · 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
avatar image guvennnnn · Feb 03, 2017 at 06:47 PM 0
Share

u r totally right. It needs NetworkIdentity and LocalPlayerAuthority

avatar image Stahhl · Dec 07, 2018 at 10:24 AM 0
Share

You helped me three three years later, bless!

avatar image
0

Answer by zentix · Jul 21, 2017 at 08:39 AM

Sir i have the same problem. CAn you please send me your project on dellqadir786@live.com

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

10 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

Related Questions

Material doesn't have a color property '_Color' 2 Answers

Stackers game logic? 0 Answers

Fade 2 out of 3 gameobjects on the screen who have the same material 2 Answers

Networking troubles with prefab 0 Answers

Changing variable value Network 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