Function Returns Objects Of Multiple Types Can't Call Methods On Returned Object

Hey all,

I want to have a function return a generic game object that I can then call a method on. This doesn’t work.

I have different objects in my game world. I’d like to have one function I can call that will return the object that was found at a given location. Then, check what type of object was found. Each of my objects has a GetObjectType method that returns the type of object.

I greatly appreciate any help you can send my way. Thanks for looking.

GameObject spotObjectLocation1;

spotObjectLocation1 = GetObjectAtLocation(player.transform.position + checkDirection);

// THIS IS WHAT I WANT TO DO THAT DOESN'T WORK
if (spotObjectLocation1.GetObjectType() == "box")
{
}

GameObject GetObjectAtLocation(Vector3 location)
{
    GameObject[] objects = GameObject.FindGameObjectsWithTag("boxes");
		
    for (int i = 0; i < objects.Length; i++)
    {
        if (objects[i].transform.position.x == location.x  objects[i].transform.position.z == location.z)
        {
            return objects[i];
        }
    }

    GameObject[] objects = GameObject.FindGameObjectsWithTag("blocks");

    for (int i = 0; i < objects.Length; i++)
    {
        if (objects[i].transform.position.x == location.x  objects[i].transform.position.z == location.z)
        {
            return objects[i];
        }
    }

}

public class ObjectBox : MonoBehaviour {
	
    public string GetObjectType()
    {
        return "box";	
    }

}

public class ObjectBlock : MonoBehaviour {
	
    public string GetObjectType()
    {
        return "block";	
    }

}

Use an interface or a common base class.

Interface approach:

public interface IObjectType
{
    string GetObjectType();
}

public class ObjectBox : MonoBehaviour, IObjectType
{
    public string GetObjectType() { return "box"; }
}

public class ObjectBlock : MonoBehaviour, IObjectType
{
    public string GetObjectType() { return "block"; }
}

Base class approach:

public class ObjectType : MonoBehaviour
{
    public virtual string GetObjectType() { return "object"; }
}

public class ObjectBox : ObjectType
{
    public override string GetObjectType() { return "box"; }
}

public class ObjectBlock : ObjectType
{
    public override string GetObjectType() { return "block"; }
}

The base class makes it easier to just “automatically” get the correct response.

ObjectType ot = go.GetComponent<ObjectType>();
Debug.Log(ot.GetObjectType());  // will print "box" or "block"

This works.

Thank you very much. If you wanna send me your PayPal address, I’ll send ya a few bucks.

Alternately, you could do an extension method on GameObject.

public static string GetObjectType(this GameObject gameObject)
{
    ObjectBox ob = gameObject.GetComponent<ObjectBox>();
    if (ob != null)
        return "box";

    ObjectBlock obl = gameObject.GetComponent<ObjectBlock>();
    if (obl != null)
        return "block";

    return null;
}

// and cal it thusly
if (spotObjectLocation1.GetObjectType() == "box")

Basically does the same thing but abstracts it out a bit.