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";
}
}