Calling an RPC from OnEnable throws a Stack Overflow Exception. What am I missing?

Hi,
So basically, I’m trying to sync the visual state of player’s spawned gun.

The way I’m doing weapon switching is by disabling and enabling the Shooter Component Network Behaviour and the visual GameObject which is the child of the Shooter Component Network Object.

So naturally, I’m doing the hiding and the showing from the OnEnable and OnDisable of the Weapon Component:

ShooterBase.cs

    [SerializeField] protected GameObject visual;
    [Rpc(SendTo.Everyone, RequireOwnership = false, DeferLocal = true)]
    private void SetVisualStateRPC(bool show)
    {
        visual.SetActive(show);
        Debug.Log("Show visual? " + show);
    }
    private void OnEnable()
    {
        SetVisualStateRPC(true);
    }
    private void OnDisable()
    {
        SetVisualStateRPC(false);
        EndFireCycle();
    }

However, this throws a Stack Overflow Exception when the player spawns:
image
Trace: StackOverflowException: The requested operation caused a stack overflow.Unity. - Pastebin.com

For context, the weapons are instantiated and network spawned when the player object spawned:

PlayerWeaponHandler.cs

public override void OnNetworkSpawn()
    {
        try
        {
            for(int i = 0; i < weaponsToSpawn.Count; i++)
            {
                if (HasAuthority)
                {
                    GameObject spawnedWeapon = Instantiate(weaponsToSpawn[i], gripAnchor.position, gripAnchor.rotation, gripAnchor);
                    shooters.Add(spawnedWeapon.GetComponent<ShooterBase>());
                    shooters[i].NetworkObject.Spawn();
                }
                shooters[i].NetworkObject.TrySetParent(NetworkObject, false);
            }
            DisableAllWeapons();
        }
        catch(Exception e) 
        {
            Debug.LogWarning("Caught error: " + e.Message + ", " + e.Source);
        }
    }

That code ran as expected before I introduced the visual hiding RPC in WeaponComponent.cs. Now, it throws the following exception:
image

Note: ProjectileShooter.cs inherits ShooterBase.cs

I guess my question is: Is the Stack Overflow exception caused by calling the RPC from OnEnable or is it something to do with the PlayerWeaponHandler.cs spawning the objects that call said RPC?

To be honest I could try debugging this on my own but It’s been a long day of trial and errors and I feel like I misunderstood some part of NGO so I would greatly appreciate any help from the community and possibly find a better approach for these kinds of things.
Thanks!

Dang, that second code is lowkey hard to read. Thought it’d have syntax highlighting. Sorry!

Oh yeah, I use Distributed Authority if that’s relevant.

Maybe, maybe not. Doesn’t really matter because you can’t call RPC methods until OnNetworkSpawn runs or afterwards because OnEnable runs before the object is initialized from a network perspective.

1 Like

I don’t know how I missed that lol. I added IsSpawned check and it seems to have fixed the problem. Thanks!