How do I sync tags, colors, and layers?

Hey so I’m new to netcode for gameobjects but I just can’t seem to figure out how to sync colors and tags with the client when I spawn a gameobject and change the tags, colors, and layers. Please Help!

  GameObject balle = Instantiate(bone, transform.position + transform.GetChild(0).transform.GetChild(0).transform.right * 2, transform.GetChild(0).transform.GetChild(0).transform.rotation);
           balle.GetComponent<Rigidbody2D>().velocity = gameObject.transform.GetChild(0).transform.GetChild(0).transform.right * 10;
           balle.layer = 6;
          balle.tag = "RedProjectile";
          balle.GetComponent<SpriteRenderer>().color = Color.red;
          var etworkObject = balle.GetComponent<NetworkObject>();
          etworkObject.Spawn(balle);
          lastone = Time.time;

That’s the kind of code that will only cause trouble, more so when networking. I mean the trainwreck of GetChild().GetChild().GetChild() (note that the transform in between is superfluous to begin with since GetChild() returns a transform).

Instead, create a field, assign the reference in the Inspector. Shorter code, easier to debug. Google if necessary, plenty of tuts on these basic lifetime saving have-to-knows.

You use RPCs for that. You can send the tag as string parameter, or the layer as an int, not sure about color - as a struct it should work but if not you may have to send each color component separately.

But generally such a thing is often overkill. If the player can pick from a preset of 20 colors, you wouldn’t send the color but rather an index into the color array that both clients share. This is generally more efficient to sync over the network for all sorts of preset data.

1 Like