Code Optimizing - Cache an Iteration From a List

Is it possible to call the component/class/script from an object within a list of another class, Without using GetComponent() (for performance reasons), and without using an Interface?

knownResources*.state != Resources.State.Carried*

The Worker.cs finds the resources via sight/collision. And then determines from out of the found objects, which to declare the workLoad , or object to focus on.
public class Worker : MonoBehaviour
{
public List knownResources;

  • public GameObject workLoad;*

  • void FindWorkLoad()*
    {

  •  for (int i = 0; i < knownResources.Count; i++)*
    

{
_ if (knownResources*.GetComponent().state != Resources.State.Carried &&_
_ knownResources.GetComponent().state != Resources.State.Inventoried)
{
workLoad = knownResources;
state = State.Working;
return;
}
}
}
}*

Multiple edits… Answer finally found…_

After much researching, testing, and headaches… I finally found the answer, to not using GetComponent() in runtime as it is slow, and multiple calls increase the slowness. And to avoid using Interfaces as they are even slower than GetComponent(). The answer is to use static variables, and quite frankly to use static lists of the Instance of the script. Not the object. As the script can easily call the Object, but the Object cannot easily reference the script. Object Oriented Programming my but… cough*

Within the Worker.cs this is what the FindWorkLoad() looks like now:

void FindWorkLoad()
    {
        if (knownEntities > 0)
        {
            if (knownResources.Count > 0)
            {
                Vector3 pos = knownResources[0].transform.position;
                if (pos.y < Global.carryHeight)
                {
                    if (DistanceTo(pos) > 1) { TurnAndMoveTo(pos); }
                    else
                    {
                        if (Resources.SharedInstance.CanSetAsCarrier(gameObject, knownResources[0]))
                        {
                            workLoad = knownResources[0];
                            state = State.Returning;
                        }
                        else { knownResources.RemoveAt(0); }
                    }
                }
                else { knownResources.RemoveAt(0); }
            }
            else
            {
                if (knownRawResources[0].activeInHierarchy)
                {
                    Vector3 pos = knownRawResources[0].transform.position;
                    if (DistanceTo(pos) > 1) { TurnAndMoveTo(pos); }
                    else
                    {
                        patienceCounter++;
                        if (patienceCounter > patience / 4)
                        {
                            throwPunch = true;
                            patienceCounter = 0;
                        }
                    }
                }
                else
                {
                    energyPerc--;
                    knownRawResources.RemoveAt(0);
                }
            }
        }
        else { state = State.Explore; }
    }

The one thing I was able to do, in my case, was to use the height of the position.Y value of the resource in question, as when it is carried it is at a set height. So no calls were needed to check if another worker is carrying the resource. Since my game changes no height on the huge map, this was easily possible. But calling and referencing the object was still needed. The Resources.cs is a parent class of the child-ed object scripts, which was also put into an empty gameObject which serves as a container for all the resources on the map. And while debugging/editor view they are hidden in it, and only show when un-collapsed. This is what Resources.cs looks like now:

// Static values do not show in inspector
    public static Resources SharedInstance; // needed to call non-static functions/methods
    public static int maxMeatsSpawn = 1;    // needed for initial instantiation
    public static List<Resources> allResources = new List<Resources>();
    public static List<Log> logs = new List<Log>();
    public static int activeLogs;
    public static List<Meat> meats = new List<Meat>();
    public static int activeMeats;
	
	 private void Awake()
    {
        SharedInstance = this;
    }
	
	public bool CanSetAsCarrier(GameObject me, GameObject it)
    {
        for (int i = 0; i < allResources.Count; i++)
        {
            if (allResources*.gameObject == it)*

{
if (allResources*.carrier == null)*
{
allResources*.carrier = me;*
return true;
} else { return false; }
}
}
return false;
}
And then the resource itself, which is Log.cs only needs this:
void Start()
{
logs.Add(this);
allResources.Add(this);
gameObject.SetActive(false);
}
After getting rid of all the GetComponent() checks I had before, to do this simple transaction of code, My game runs much smoother, and only loses roughly 2 fps, versus the original value of up to and over 15 fps. If you are able to use static values like this, this is considered caching, and it is definitely faster. Thankfully I found an answer to this, as my game can include much much more now, before being hindered by standard coding limits. :D