Search Unity

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

Discussion in 'Multiplayer' started by thesupersoup, Apr 15, 2018.

  1. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    65
    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:
    Code (CSharp):
    1. [ClientRpc]
    2. 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
    3. {
    4.     Collider[] localEnts = Physics.OverlapSphere(this.transform.position, MAX_DIST, mask);
    5.     if(localEnts != null && localEnts.Length > 0)
    6.     {
    7.         foreach(Collider col in localEnts)
    8.         {
    9.             Stats tempStats = col.gameObject.GetComponent<Stats>();
    10.              
    11.             if(tempStats != null)
    12.             {
    13.                 if(tempStats.GetName() != entStats.GetName()) // NPCs aren't worried about same species... for now.
    14.                 {
    15.                     if(!trackBook.Contains(col.gameObject))
    16.                         trackBook.Add(col.gameObject);
    17.                 }
    18.             }
    19.             else
    20.                 Debug.LogWarning(this.gameObject + " RpcAreaScan(): Error fetching the Stats script for a detected entity, can't add to trackBook");
    21.         }
    22.     }
    23. }
    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!
     
  2. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    65
    So I did a little Debug.Log work and fixed the problem. Here's what the script looks like now:

    Code (CSharp):
    1. [ClientRpc]
    2. 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
    3. {
    4.     Collider[] localEnts = Physics.OverlapSphere(this.transform.position, MAX_DIST, mask);
    5.     if(localEnts != null && localEnts.Length > 0)
    6.     {
    7.         foreach(Collider col in localEnts)
    8.         {
    9.             Stats tempStats = col.gameObject.GetComponent<Stats>();
    10.                
    11.             Debug.Log("Detected a " + col.gameObject);
    12.                
    13.             if(tempStats != null)
    14.             {
    15.                 if(tempStats.GetName() != entStats.GetName()) // NPCs aren't worried about same species... for now.
    16.                 {
    17.                     if(!trackBook.Contains(col.gameObject))
    18.                     {
    19.                         Debug.Log("Adding " + col.gameObject + " to the trackBook");
    20.                         trackBook.Add(col.gameObject);
    21.                     }
    22.                     else
    23.                         Debug.Log(col.gameObject + " is already in the trackBook");
    24.                 }
    25.             }
    26.             else
    27.                 Debug.LogWarning(this.gameObject + " RpcAreaScan(): Error fetching the Stats script for a detected entity, can't add to trackBook");
    28.         }
    29.     }
    30. }
    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.
     
unityunity