C# Variable not updating properly.

I’m doing a multiplayer FPS in Unity5, and I have this code to move the local player:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class PlayerController : NetworkBehaviour
	public GameObject bulletPrefab;
	public Transform bulletSpawn;
	public int fireLocked;
	void Start(){
		fireLocked = 0;
	void FixedUpdate()
		if (!isLocalPlayer)

		var x = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0f;
		var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

		if (Input.GetKeyDown("space")){
			this.GetComponent<Rigidbody>().AddForce(new Vector3(0f,10f,0f));

		transform.Translate(x, 0, z);
		transform.Rotate (new Vector3 (0.0f,Input.GetAxis ("Mouse X")*5.0f,0.0f));

		if (Input.GetMouseButtonDown(0))
			if (fireLocked==0) {
				Debug.Log ("fired");
				CmdFire ();

	// This [Command] code is called on the Client …
	// … but it is run on the Server!
	void CmdFire()
		// Create the Bullet from the Bullet Prefab
		var bullet = (GameObject)Instantiate (

		// Add velocity to the bullet
		bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 6;

		// Spawn the bullet on the Clients
		NetworkServer.Spawn (bullet);

		// Destroy the bullet after 2 seconds
		Destroy (bullet, 2.0f);

	public override void OnStartLocalPlayer ()
		if (isLocalPlayer) {
			FindObjectOfType<Camera> ().gameObject.transform.position = this.transform.position+new Vector3(0f,0.5f,0f);
			FindObjectOfType<Camera>().gameObject.transform.SetParent (this.transform);
	public void LockFire(){
		bLockFire ();

	public void UnLockFire(){
		bUnLockFire ();
	void bLockFire(){
		fireLocked = 1;
		Debug.Log (fireLocked);
	void bUnLockFire(){
		fireLocked = 0;
		Debug.Log (fireLocked);

Other class (named Shop) calls the LockFire void. That void is supposed to change fireLocked to 1, and it does, but later, in the FixedUpdate void, fireLocked is 0.

I have debugged, and the results show up that fireLocked changes to 1 in the LockFire void, but it doesn’t in the FixedUpdate void.

Even if I do player.GetComponent ().fireLocked = 1; from the Shop script, it doesn’t seem to work.

Also, UnLockFire ISN’T called.

Why is this? How do I change the fireLocked variable too in the FixedUpdate void?

P.S: The value of fireL NEVER changes from zero in the FixedUpdate void, and, if you change FixedUpdate() to LateUpdate, it’s the opposite: It never changes from 1 for no reason, althrough LockFire() hasn’t been called…

EDIT: Now it works! I’ve replaced FixedUpdate() to LateUpdate() And then I switched back to FixedUpdate(), and it worked! I think it was an Unity problem?

But later, I removed the Lockfire voids, because I was changing FireL directly with player.GetComponent ().fireLocked = 1. Then, it didn’t work. Whaaaaat?

This is an unity3d problem for sure.

Ok, this will not be only the answer to your question, but I have found a few critical flaws in your networking code. First let’s clear the locked thing first.
fireLocked, where is it changed? On client, or on the server, that is the question you should ask yourself. What worries me is this player.GetComponent ().fireLocked = 1;. It is weird, it gets nowhere, did you mean player.GetComponent<PlayerController> ().fireLocked = 1;? It should work now, but keep doing it using the functions inside the script.

If it is only bLockFire and bUnLockFire that change the value, then debug which ones get called. If both get called, then you have to go to fix your shop script first.

If it still does not work, then there is no other explanation for that, only some dumb mistakes occured. And they happen to everyone really often, trust me.

Now to the networking part. Basically you check for the value inside the client, and then tell the server to do the stuff. Congratulations! You have just made it possible for crackers to shoot hundreds of bullets a second and crash the computers of every client. The server should check for the value, not the client. The server should handle weapon locking, not the client. Use [SyncVar] just to let the player know what happens, but never trust the player.

And if you do everything in server, you will not have to worry where was the value changed (but you will have to worry about other things instead, hehe).

I found a fix!!

I putted this code in fireLock.cs (A script that now just shoots):

net.client.connection.playerControllers [0].gameObject.GetComponent<FireLock> ().LockFire ();

net.client.connection.playerControllers [0].gameObject.GetComponent<FireLock> ().UnLockFire ();