Search Unity

NetworkObjectReference Not working when using steamworks ?

Discussion in 'Netcode for GameObjects' started by edin97, May 7, 2022.

  1. edin97

    edin97

    Joined:
    Aug 9, 2021
    Posts:
    58
    Hello, I have this "tower" GameObject, it has a networkobject component on it since it has a health bar that needs to be updated for all players. Now, to apply damage to the tower with a spell, I'm using a ServerRpc on collision between the spell and the tower, so OnTriggerEnter has access to the tower's networkObject component. Also the tower Gameobject is not on the networkManager list because the host and client are not even able to connect when the tower is on that list.

    I'm calling the ServerRpc giving it the NetworkObjectReference :
    Code (CSharp):
    1.     [ServerRpc]
    2.     private void ApplyDamageToTowerFromServerRpc(NetworkObjectReference towerNetworkObject, float damageDoneAmount)
    3.     {
    4.         if (towerNetworkObject.TryGet(out NetworkObject targetObject))
    5.         {
    6.             if (targetObject.IsOwner)
    7.             {
    8.                 if (targetObject.gameObject.TryGetComponent(out TowerHealthManager towerHealthManager))
    9.                 {
    10.                     towerHealthManager.ApplyDamage(damageDoneAmount);
    11.                 }
    12.             }
    13.         }
    14.     }
    15.  
    16.     private void OnTriggerEnter(Collider other) // -> Other is the Tower, this script is on the spell
    17. {
    18.         if (!IsOwner){return;}
    19. (....)
    20.         if (other.TryGetComponent(out TowerHealthManager towerHealthManager))
    21.         {
    22.             if (other.gameObject.GetComponent<NetworkObject>().NetworkManager.IsServer)
    23.             {
    24.                 towerHealthManager.ApplyDamage(SpellToCast.DamageAmount);
    25.             }
    26.             else
    27.             {
    28.                 ApplyDamageToTowerFromServerRpc(other.gameObject.GetComponent<NetworkObject>(), SpellToCast.DamageAmount); //->issue here
    29.             }
    30.        }
    31.  
    This works perfectly when playing without Steam : -> Using ParralelSync to make a copy of my game and join myself on my own PC. Both client and host have their tower health updated

    When using Steam, this is the error message I get :

    Code (CSharp):
    1.  
    2. ArgumentException: NetworkObjectReference can only be created from spawned NetworkObjects.
    3. Unity.Netcode.NetworkObjectReference..ctor (Unity.Netcode.NetworkObject networkObject) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.6/Runtime/Serialization/NetworkObjectReference.cs:42)
    4. Unity.Netcode.NetworkObjectReference.op_Implicit (Unity.Netcode.NetworkObject networkObject) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.6/Runtime/Serialization/NetworkObjectReference.cs:125)
    5. Spell.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Mobile/Spells/Spell.cs:586)
    6.  
    I do understand that the issue is my Tower not been instantiated, but it been already in the scene. My question is, why is this not working with steam but works with port forwarding ? Is there an easier way to resolve this ? Instantiating would make everything overcomplicated for me so hopefully there is a solution.


    Edit : OK, I was using a NetworkVariable<float> to keep track of my tower health. But apparently there is no need for NetworkVariables or ServerRpc if the GameObject is not instantiated (already in the scene before pressing play). I changed that to a normal float. My other issue (which is why I was using NetworkVariable in the first place) was this line : "if (!IsOwner){return;}" which is correct I think for instantiated GameObjects. I removed that and now it all works because both client and host are applying damage on their side. It just feels weird because it means the client can cheat (?) since the client and host both have a their health variable which are not connected
     
    Last edited: May 7, 2022