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
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)
{
//...
}
}