Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Call Rpc Not Working

Discussion in 'Netcode for GameObjects' started by dawudfc2014, Mar 10, 2023.

  1. dawudfc2014

    dawudfc2014

    Joined:
    Apr 28, 2022
    Posts:
    4
    I've been trying to get my bullets to sync across my Netcode for game objects server using RPCs (since the bullets aren't the server). No matter what I do it will not run, debugging and profile analysis shows that RPC bytes are being sent but not received. Any help is appreciated and thanks in advance
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine.Networking;
    5. using UnityEngine;
    6. using System;
    7.  
    8. public class Bullet :   NetworkBehaviour
    9. {
    10.     public float speed = 10f; // Speed of the bullet
    11.     public float damage = 10f; // Damage inflicted by the bullet
    12.  
    13.     public float lifetime = 2f; // Time in seconds before the bullet is destroyed
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         Debug.Log("Called");
    19.         NetworkCallSpawn();
    20.     }
    21.  
    22.     private void NetworkCallSpawn()
    23.     {
    24.         Debug.Log("2ndCall");
    25.         NetworkSpawnServerRpc();
    26.     }
    27.  
    28.     [ServerRpc(RequireOwnership = false)]
    29.     public void NetworkSpawnServerRpc(ServerRpcParams rpcParams = default)
    30.     {
    31.         Debug.Log("Success");
    32.         Debug.Log("BulletCallSERVER");
    33.         this.GetComponent<NetworkObject>().Spawn();
    34.     }
    35.  
    36.     public void OnTriggerEnter2D(Collider2D other)
    37.     {
    38.  
    39.         Debug.Log("BulletImpact");
    40.         // Check if the bullet has collided with a player
    41.         PlayerMovement player = other.GetComponent<PlayerMovement>();
    42.         if (player != null)
    43.         {
    44.             DestroyBulletServerRpc();
    45.             // Inflict damage to the player and destroy the bullet
    46.             player.TakeDamage(damage);
    47.             Debug.Log("Hit");
    48.         }
    49.  
    50.         if (other.GetComponent<BulletDeflector>() == null)
    51.         {
    52.  
    53.             DestroyBulletServerRpc();
    54.             Debug.Log("Wall");
    55.            
    56.         }
    57.         else
    58.         {
    59.             Debug.Log("Deflect");
    60.             return;
    61.         }
    62.        
    63.  
    64.     }
    65.  
    66.     [ServerRpc(RequireOwnership = false)]
    67.     public void DestroyBulletServerRpc(ServerRpcParams rpcParams = default)
    68.     {
    69.         Debug.Log("Destroyed");
    70.         GameObject bulletthis = this.gameObject;
    71.         bulletthis.GetComponent<NetworkObject>().Despawn();
    72.         Destroy(gameObject);
    73.     }
    74.  
    75. }
    76.  
    Above is bullet script and below is shooting script
    Code (CSharp):
    1. using System.Globalization;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4. using Unity.Netcode;
    5. using System.Collections.Generic;
    6.  
    7. public class Shooting : NetworkBehaviour
    8. {
    9.  
    10.     public Transform firePoint;
    11.     public GameObject bulletPrefab;
    12.     [SerializeField]private List<GameObject> bullets = new List<GameObject>();
    13.  
    14.     public float bulletForce = 20f;
    15.     private ulong clientId;
    16.  
    17.     private void Start()
    18.     {
    19.         clientId = bulletPrefab.GetComponent<NetworkObject>().OwnerClientId;
    20.     }
    21.  
    22.     public void OnShoot()
    23.     {
    24.         FireBulletServerRpc();
    25.     }
    26.    
    27.  
    28.     [ServerRpc(RequireOwnership = false)]
    29.     public void FireBulletServerRpc(ServerRpcParams rpcParams = default)
    30.     {
    31.         Debug.Log("Shoot" + this.GetComponent<NetworkObject>().OwnerClientId);
    32.         if (!IsOwner) return;
    33.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    34.         bullets.Add(bullet);
    35.        
    36.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    37.         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    38.     }
    39.  
    40.  
    41. }
    42.  
     
  2. dawudfc2014

    dawudfc2014

    Joined:
    Apr 28, 2022
    Posts:
    4
    Solved it! you need to do this.gameObject.GetComponent<NetworkObject>().Spawn() in the shoot script instead