I need help understanding the use of abstract classes. I have an abstract class called WorldObject:
public abstract class WorldObject
{
public Vector3 position;
}
I have many classes that inherit from this, for example Building and Tree.
I want to create a method that loops through a List of WorldObjects, and return the closest one to me.
I want to be able to use this method with all my types of WorldObjects, but I can't get it to work:
public List<Building> buildings = new List<Building>();
public List<Tree> trees= new List<Tree>();
private void GoToClosestBuilding()
{
var target = GetClosestWorldObject(buildings); //Error: Cannot convert building to WorldObject
}
Vector3 GetClosestWorldObject(List<WorldObject> objects)
{
//...
}
When I try to pass my Building or Tree lists to my method, it returns an error that it cannot convert between Building and WorldObject, and so on.
How can I structure my code so that something like this would work?