Networking a gameobjects color change

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class CapController : NetworkBehaviour
{

    public bool blueTeam = false;
    public bool redTeam = false;


    private float redcaptureperc = 0;
    private float bluecaptureperc = 0;

    public GameObject flagRed;
    public GameObject flagBlue;
    public GameObject flagNeutral;

    public Rect capturebox = new Rect(10, 10, 300, 25);

    void Start()
    {
        flagNeutral.GetComponent<MeshRenderer>().enabled = true;
    }
    [Server]
    public void ServerUpdate()
    {
        if (redcaptureperc != 100)
        {
            flagRed.GetComponent<MeshRenderer>().enabled = true;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;
        }
       
        if (bluecaptureperc != 100)
        {
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = true;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;
        }

        if ((redcaptureperc >= 50 && bluecaptureperc >= 51) || (redcaptureperc >= 51 && bluecaptureperc >= 50))
        {
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = true;
        }
    }

   
    [ClientRpc]

    public void RpcUpdate()
    {
        if (redcaptureperc != 100)
        {
            flagRed.GetComponent<MeshRenderer>().enabled = true;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;
        }

        if (bluecaptureperc != 100)
        {
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = true;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;
        }

        if ((redcaptureperc <= 50 && bluecaptureperc <= 51) || (redcaptureperc >= 51 && bluecaptureperc >= 50))
        {
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = true;
        }
    }


    void Update()
    {
        if (blueTeam == true)
        {
            bluecaptureperc += Time.deltaTime * 20;
            redcaptureperc -= Time.deltaTime * 20;
        }

        if (redTeam == true)
        {
            redcaptureperc += Time.deltaTime * 20;
            bluecaptureperc -= Time.deltaTime * 20;
        }

        if (redTeam == true && blueTeam == true)
        {
            redcaptureperc = redcaptureperc;
            bluecaptureperc = bluecaptureperc;
        }

        if (redcaptureperc >= 100)
        {
            redcaptureperc = 100;
            flagRed.GetComponent<MeshRenderer>().enabled = true;      
            flagBlue.GetComponent<MeshRenderer>().enabled = false;    
            flagNeutral.GetComponent<MeshRenderer>().enabled = false; 

        }

        if (bluecaptureperc >= 100)
        {
            bluecaptureperc = 100;
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = true;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;

        }

        if ((redcaptureperc <= 50 && bluecaptureperc <= 51) || (redcaptureperc >= 51 && bluecaptureperc >= 50))
        {
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = true;

        }

        if (redcaptureperc <= 0)
        {
            redcaptureperc = 0;
        }

        if (bluecaptureperc <= 0)
        {
            bluecaptureperc = 0;
        }
    }
   
    void OnGUI()
    {
        GUI.Box(capturebox, "Red Cap " + ((int)redcaptureperc).ToString() + " Blue Cap " + ((int)bluecaptureperc).ToString());
    }
}

My problem is the flags meshrenderer isnt sent to all the clients. I.E: Player tagged red captures the flag after some seconds flag turns red, but for the other client its still the color as flagNeutral. I’ve been struggling for a couple of days and been trying to understand something from the unity manual but no luck. Please help.

Help?

bump…

You never call ServerUpdate or RpcUpdate. Decorating them with attributes doesn’t make them magically run. There are a few ways to do this but the method I would use would be for the server do all of the percentage calculations and then inform all clients when a team captures something. So in Update I’d do this

if (!isServer)
    return;

// then the rest of your percent calculation code

if (redPercent >= 100)
{
    CmdSetRed();
}
else if (bluePercent >= 100)
{
    CmdSetBlue();
}

Those commands go to the server, who would then in turn notify all clients

[Command]
void CmdSetRed()
{
    RpcSetRed();
}

[ClientRpc]
void RpcSetRed()
{
    blueTeam = false;
    redTeam = true;
    // turn your renderers on etc
}

Generally speaking, there’s a lot of cleanup you could do with that code as well. Instead of storing GameObjects and using GetComponent() all the time just store the MeshRenderer directly. Turn the two booleans into an enum to represent current ownership. Lines 93-97 literally don’t do anything.

The other way to do it would be to turn the percentages into SyncVars that you check in Update for everyone.

like this?

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class CaptureController : NetworkBehaviour
{
    public bool blueTeam = false;
    public bool redTeam = false;


    public GameObject flagRed;
    public GameObject flagBlue;
    public GameObject flagNeutral;

    public float bluecapturepercent = 0;
    public float redcapturepercent = 0;
    public Rect capturebox = new Rect(10, 10, 300, 25);


    void Update()
    {
        if (!isServer)
            return;
        if (blueTeam == true)
        {
            bluecapturepercent += Time.deltaTime * 20;
            redcapturepercent -= Time.deltaTime * 20;
        }

        if (redTeam == true)
        {
            redcapturepercent += Time.deltaTime * 20;
            bluecapturepercent -= Time.deltaTime * 20;
        }

        if (redTeam == true && blueTeam == true)
        {
            redcapturepercent = redcapturepercent;
            bluecapturepercent = bluecapturepercent;
        }

        if (redcapturepercent >= 100)
        {
            redcapturepercent = 100;
            flagRed.GetComponent<MeshRenderer>().enabled = true;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;

        }

        if (bluecapturepercent >= 100)
        {
            bluecapturepercent = 100;
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = true;
            flagNeutral.GetComponent<MeshRenderer>().enabled = false;

        }

        if ((redcapturepercent <= 50 && bluecapturepercent <= 51) || (redcapturepercent >= 51 && bluecapturepercent >= 50))
        {
            flagRed.GetComponent<MeshRenderer>().enabled = false;
            flagBlue.GetComponent<MeshRenderer>().enabled = false;
            flagNeutral.GetComponent<MeshRenderer>().enabled = true;

        }

        if (redcapturepercent <= 0)
        {
            redcapturepercent = 0;
        }

        if (bluecapturepercent <= 0)
        {
            bluecapturepercent = 0;
        }


        if (redcapturepercent >= 100)
        {
            CmdSetRed();
        }
        else if (bluecapturepercent >= 100)
        {
            CmdSetBlue();
        }
    }

        [Command]
    void CmdSetRed()
    {
        RpcSetRed();
    }
    [Command]

    void CmdSetBlue()
    {
        RpcSetBlue();
    }

    [ClientRpc]
    void RpcSetRed()
    {
        blueTeam = false;
        redTeam = true;

        flagRed.GetComponent<MeshRenderer>().enabled = true;
    }

    [ClientRpc]
    void RpcSetBlue()
    {
        blueTeam = true;
        redTeam = false;

        flagBlue.GetComponent<MeshRenderer>().enabled = true;
    }
    void OnGUI()
    {
        GUI.Box(capturebox, "Red Cap " + ((int)redcapturepercent).ToString() + " Blue Cap " + ((int)bluecapturepercent).ToString());
    }
}

i dont know how to make bool into enum

enum TeamOwner { Red, Blue };

TeamOwner owner;

[Command]
void CmdSetOwner(int owner)
{
    RpcSetOwner(owner);
}

[ClientRpc]
void RpcSetOwner(int owner)
{
    this.owner = (TeamOwner)owner;
    if (owner == TeamOwner.Red)
    {
        flagRed.enabled = true;
    }
    else
    {
        flagBlue.enabled = true;
    }
}

This isn’t working. the flag turns red on the client that captured the flag and on the other client it turns blue why so?

Do i have to add networkidentities to the flags and make those server only also? they are child’s in a pole so is the capturecontroller

Edit: maybe this script is interfering

using UnityEngine;
using System.Collections;

public class FlagCap : MonoBehaviour {

    private CaptureController capController;

    void Start()
    {
        capController = GameObject.Find("CaptureController").GetComponent<CaptureController>();
    }

    void OnTriggerEnter(Collider Col)
    {
        if(Col.tag == "Blue")
        {
            capController.blueTeam = true;
        }

        if(Col.tag == "Red")
        {
            capController.redTeam = true;
        }
    }

    void OnTriggerExit(Collider Col)
    {
        if(Col.tag == "Blue")
        {
            capController.blueTeam = false;
        }

        if(Col.tag == "Red")
        {
            capController.redTeam = false;
        }
    }
}