Unity Netcode for GameObjects variable not synchronized within the same script

Hi everyone,

I have troubles sorting out this issue. I tried everything that comes to my mind and mind of others on various number of this and other forums.

To shortly describe issue:
On OnTriggerEnter player enters object and passes check. If successful the network variables canPickUpNetwork.Value gets set to True;

Then in my other InputManager script once player presses space bar the function *PickUpCheck()*gets executed.

On the very start of that script same variable canPickUpNetwork.Value is seen as False?

And so whole if structure gets skipped and function is, well not functioning.

I would be happy to receive any help and/or suggestions to solve this issue. I provided shortened script so its easier to understand and read. If anybody needs a whole script, I will happily provide it.

Greetings and thank you in advance :sweat_smile: :slightly_smiling_face:

public class GrabManager : NetworkBehaviour
{
    //Few more variables...

    private GameObject boxToPickUp;  
    private bool canPickUp = false;
    private bool isPickedUp = false;

    private NetworkVariable<bool> canPickUpNetwork = new NetworkVariable<bool>(false, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

    public void PickUpCheck()
    {
        Debug.Log("<color=orange>Script communication check.</color>");
        Debug.Log("<color=orange>Variables -> canPickUp:" + canPickUpNetwork.Value + "; isPickedUp:" + isPickedUp + "</color>");

        if (!canPickUpNetwork.Value || boxToPickUp == null)
        {
            Debug.Log("<color=red>Cannot pick up: No valid object detected.</color>");
            return;
        }

        // Ensuring the object has ownership and is ready to be picked up
       // Point of failure

        NetworkObject networkObject = boxToPickUp.GetComponent<NetworkObject>();

        if (isPickedUp && boxToPickUp != null)
        {
            //DROP OFF
            
        }

        if (networkObject != null && networkObject.IsOwner)
        {
            //PICK UP
            Debug.Log("<color=red>Pick up !</color>");
           // Never got it to work yet...
            }
        }
        else
        {
            Debug.Log("<color=red>Cannot pick up: Object not owned by player.</color>");
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (!IsOwner) return;

        if (other.CompareTag("Pickupable"))
        {
            NetworkObject networkObject = other.GetComponent<NetworkObject>();

            if (networkObject != null && networkObject.IsOwner)
            {
                Debug.Log($"Can pick up: {other.gameObject.name}");

                boxToPickUp = other.gameObject;

                canPickUpNetwork.Value = true;
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
       //...
    }

}

What is the “very start of that script”? Is it the Start() method? This may be too early since the object may not have spawned on the network yet. Be sure to do any netcode initialization in or after OnNetworkSpawn.

Question: why are you synchronizing whether something can be picked up?

Each client can run its own OnTriggerEnter to check if item pickup is possible for them as their position may slightly differ (due to latency) compared to other clients or the host/server. Only when then client wants to pick up an item is where you can (optionally) verify on the server-side whether that client is allowed to pickup that particular item to prevent cheats of the sort that would allow clients to pick up objects at a large distance or behind walls.

Hi CodeSmile,

Thanks for fast respond.

There are no other functions in the script before nor after those presented in the code snippet.

Question: why are you synchronizing whether something can be picked up?

It was my desperate attempt of just getting a variable to be the same and to try to see will it then be synced. It will be rewritten to normal variable.

The way structure of my code goes and overall goal of this part of the project is for players to be able to carry and drop boxes around. Also once players do join, box they are trying to pick up is not spawned. But one of them has to press a key to spawn it ( just for testing purposes, at later point that will be automatic as a port of a gameplay). I tested with 2 games running. And so far movement and box spawning are both synced.

This is my first time developing the game that is multiplayer, and I had the code normally working before. Since I first developed portion of a game for single player but then chose to make it co-op… would be probably better if I would have started it online from the beginning…

If there is anything else you need to be able to help, or any further questions, I would be glad to hear :v::blush: