[Solved]Can't fetch script from networked GameObject that certainly has one

Hello,

So my problem probably has a simple answer, but I’ve been staring at the code so long the words have lost all meaning.

Please consider the following code block:

[ClientRpc]
public void RpcAreaScan() // Uses a Physics.OverlapSphere to scan the area up to this entity's Awareness Max Distance and log any GameObjects on Layers specified in the LayerMask
{
    Collider[] localEnts = Physics.OverlapSphere(this.transform.position, MAX_DIST, mask);
    if(localEnts != null && localEnts.Length > 0)
    {
        foreach(Collider col in localEnts)
        {
            Stats tempStats = col.gameObject.GetComponent<Stats>();
             
            if(tempStats != null)
            {
                if(tempStats.GetName() != entStats.GetName()) // NPCs aren't worried about same species... for now.
                {
                    if(!trackBook.Contains(col.gameObject))
                        trackBook.Add(col.gameObject);
                }
            }
            else
                Debug.LogWarning(this.gameObject + " RpcAreaScan(): Error fetching the Stats script for a detected entity, can't add to trackBook");
        }
    }
}

This is called on NPCs during their FixedUpdate as a part of a larger routine where they check their vicinity up to MAX_DIST for potential targets. They’re specifically searching for a script, Stats, on the detected entity. This serves as a little check as well, in case they detect something unexpected that they shouldn’t have. (Maybe I forgot to put a detail GameObject on the right layer, or some such. Don’t want NPCs staring at flowers.)

Stats is a NetworkBehaviour derived class that stores key info like the NPCs name (species), health, damage output, etc.

Anyways, the player object has a Stats script, right there on the root GameObject. And yet the NPCs can’t seem to find it. When I walk within range, I get spammed with that LogWarning at the bottom. I can’t figure out for the life of me why they can’t see the Stats script, so I have come to the community for aid.

Thanks for your time!

So I did a little Debug.Log work and fixed the problem. Here’s what the script looks like now:

[ClientRpc]
public void RpcAreaScan() // Uses a Physics.OverlapSphere to scan the area up to this entity's Awareness Max Distance and log any GameObjects on Layers specified in the LayerMask
{
    Collider[] localEnts = Physics.OverlapSphere(this.transform.position, MAX_DIST, mask);
    if(localEnts != null && localEnts.Length > 0)
    {
        foreach(Collider col in localEnts)
        {
            Stats tempStats = col.gameObject.GetComponent<Stats>();
               
            Debug.Log("Detected a " + col.gameObject);
               
            if(tempStats != null)
            {
                if(tempStats.GetName() != entStats.GetName()) // NPCs aren't worried about same species... for now.
                {
                    if(!trackBook.Contains(col.gameObject))
                    {
                        Debug.Log("Adding " + col.gameObject + " to the trackBook");
                        trackBook.Add(col.gameObject);
                    }
                    else
                        Debug.Log(col.gameObject + " is already in the trackBook");
                }
            }
            else
                Debug.LogWarning(this.gameObject + " RpcAreaScan(): Error fetching the Stats script for a detected entity, can't add to trackBook");
        }
    }
}

Turns out I was getting the error from a collider attached to a weapon. I removed the collider (Raytrace fire script y’all) and everything is working swimmingly now.

Hopefully my tribulation helps someone out there.